1.PDO Introduction
PDO (PHP Data Object) is something added to PHP 5, which is a significant feature of PHP 5, since php4/php3 before PHP 5 is a bunch of database extensions to connect and process with each database, what Php_mysql.dll, Php_ Pgsql.dll, Php_mssql.dll, Php_sqlite.dll and so on.
The PHP6 will also be connected by default using PDO, and the MySQL extension will be used as a secondary
2.PDO Configuration
PHP.ini, remove the "Extension=php_pdo.dll" in front of the ";" To connect to the database, you also need to remove the ";" in front of the database extension associated with PDO. And then restart the Apache server.
Extension=php_pdo.dll
Extension=php_pdo_mysql.dll
Extension=php_pdo_pgsql.dll
Extension=php_pdo_sqlite.dll
Extension=php_pdo_mssql.dll
Extension=php_pdo_odbc.dll
Extension=php_pdo_firebird.dll
......
3.PDO connecting MySQL Database
New PDO ("Mysql:host=localhost;dbname=db_demo", "Root", "");
The default is not a long connection, to use a long database connection, you need to add the following parameters at the end:
New PDO ("Mysql:host=localhost;dbname=db_demo", "Root", "" "," "Array (Pdo::attr_persistent = True)");
4.PDO common method and its application
Pdo::query () is primarily used for operations that have logged results returned, especially the select operation
Pdo::exec () is primarily for operations returned without a result set, such as INSERT, update, and so on
Pdo::lastinsertid () returns the last insert operation, the primary key column type is the final self-increment ID
Pdostatement::fetch () is used to get a record
Pdostatement::fetchall () is to get all recordsets into one
5.PDO operating MySQL DB instance
Copy the Code code as follows:
$pdo = new PDO ("Mysql:host=localhost;dbname=db_demo", "Root", "");
if ($pdo, exec ("INSERT into Db_demo (name,content) VALUES (' title ', ' content ')") {
echo "Insert success! ";
Echo $pdo-Lastinsertid ();
}
?>
Copy the Code code as follows:
$pdo = new PDO ("Mysql:host=localhost;dbname=db_demo", "Root", "");
$rs = Query ("SELECT * from Test"), $pdo
while ($row = Fetch () $rs) {
Print_r ($row);
}
?>
The above describes the JDBC Connection database PHP5 using PDO to connect to the database, including the JDBC Connection database content, I hope to be interested in PHP tutorial friends helpful.