On the way here on horseback - PDO connection error
Good things come hard, but there are some problems connecting to the database, which sometimes makes people angry.
1. Confirm that PDO is turned on
PDO must be turned on in php 5.1 or above version under Windows environment.
<?php print phpinfo(); ?>
php.ini configuration file and find
extension=php_pdo.dll(configuration php configuration file, Turn on the corresponding extension) and
extension=php_pdo_mysql.dll (turn on the extension to the corresponding database, take MySQL as an example), remove the previous "
;" comment, and modify the two lines of configuration The content is as follows:
extension=php_pdo.dll extension=php_pdo_mysql.dll
apache.
2. Database connection problem
- Connect to
MySQL
<?php $dbh = new PDO('mysql:host=localhost;dbname=my_database', $user, $pass); ?>
PDOException exception object will be thrown.
<?phptry { $dbh = new PDO('mysql:host=localhost;dbname=my_database', $user, $pass); foreach($dbh->query('SELECT * from student') as $row) { print_r($row); } $dbh = null;} catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>"; die();} ?>
3. There are errors in the SQL statement itself
<?php //实例化PDO对象 $pdo = new PDO('mysql:host=localhost;port=3306;dbname=my_database','root','root'); //写操作 $sql = "delete from student"; //错误SQL $rows = $pdo->exec($sql); //错误判定:exec方法执行结果成功也存在返回0的情况,错误会返回false,所以要判定是否是SQL错误,需要判定结果为false if(false === $rows){ //取出错误细信息 echo 'SQL错误:<br/>'; echo '错误代码为:' . $pdo->errorCode() . '<br/>'; echo '错误原因为:' . $pdo->errorInfo()[2]; //errorInfo返回数组,2下标代表错误具体信息 exit; //错误不需要继续执行代码 } ?>
database , confirm the statement problem through the
cmd command line or database visualization software (such as
Navicat, phpMyAdmin).
php tutorial,php video tutorial
The above is the detailed content of On the way here on horseback - PDO connection error. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a popular web development language that has been used for a long time. The PDO (PHP Data Object) class integrated in PHP is a common way for us to interact with the database during the development of web applications. However, a problem that some PHP developers often encounter is that when using the PDO class to interact with the database, they receive an error like this: PHPFatalerror:CalltoundefinedmethodPDO::prep

As a popular programming language, PHP is widely used in the field of web development. Among them, PHP's PDO_PGSQL extension is a commonly used PHP extension. It provides an interactive interface with the PostgreSQL database and can realize data transmission and interaction between PHP and PostgreSQL. This article will introduce in detail how to use PHP's PDO_PGSQL extension. 1. What is the PDO_PGSQL extension? PDO_PGSQL is an extension library of PHP, which

PHP and PDO: How to perform batch inserts and updates Introduction: When using PHP to write database-related applications, you often encounter situations where you need to batch insert and update data. The traditional approach is to use loops to perform multiple database operations, but this method is inefficient. PHP's PDO (PHPDataObject) provides a more efficient way to perform batch insert and update operations. This article will introduce how to use PDO to implement batch insert and update operations. 1. Introduction to PDO: PDO is PH

PHP and PDO: How to query and display data in pages When developing web applications, querying and displaying data in pages is a very common requirement. Through paging, we can display a certain amount of data at a time, improving page loading speed and user experience. In PHP, the functions of paging query and display of data can be easily realized using the PHP Data Object (PDO) library. This article will introduce how to use PDO in PHP to query and display data by page, and provide corresponding code examples. 1. Create database and data tables

PHP and PDO: How to perform a full-text search in a database In modern web applications, the database is a very important component. Full-text search is a very useful feature when we need to search for specific information from large amounts of data. PHP and PDO (PHPDataObjects) provide a simple yet powerful way to perform full-text searches in databases. This article will introduce how to use PHP and PDO to implement full-text search, and provide some sample code to demonstrate the process. first

PDOPDO is an object-oriented database access abstraction layer that provides a unified interface for PHP, allowing you to use the same code to interact with different databases (such as Mysql, postgresql, oracle). PDO hides the complexity of underlying database connections and simplifies database operations. Advantages and Disadvantages Advantages: Unified interface, supports multiple databases, simplifies database operations, reduces development difficulty, provides prepared statements, improves security, supports transaction processing Disadvantages: performance may be slightly lower than native extensions, relies on external libraries, may increase overhead, demo code uses PDO Connect to mysql database: $db=newPDO("mysql:host=localhost;dbnam

PHP and PDO: How to handle JSON data in databases In modern web development, processing and storing large amounts of data is a very important task. With the popularity of mobile applications and cloud computing, more and more data are stored in databases in JSON (JavaScript Object Notation) format. As a commonly used server-side language, PHP's PDO (PHPDataObject) extension provides a convenient way to process and operate databases. Book

1. Introduction to PDO PDO is an extension library of PHP, which provides an object-oriented way to operate the database. PDO supports a variety of databases, including Mysql, postgresql, oracle, SQLServer, etc. PDO enables developers to use a unified API to operate different databases, which allows developers to easily switch between different databases. 2. PDO connects to the database. To use PDO to connect to the database, you first need to create a PDO object. The constructor of the PDO object receives three parameters: database type, host name, database username and password. For example, the following code creates an object that connects to a mysql database: $dsn="mysq
