1. Introduction to PDO
PDO (PHP Data Object) is something added in PHP 5. It is a major new feature added in PHP 5, because before PHP 5, php4/php3 It is a bunch of database extensions to connect and process various databases, such as php_mysql.dll, php_pgsql.dll, php_mssql.dll, php_sqlite.dll, etc.
PHP6 will also use PDO to connect by default, and the mysql extension will be used as an auxiliary
2. PDO configuration
In PHP.ini, remove the front of "extension=php_pdo.dll" ";" sign, if you want to connect to the database, you also need to remove the ";" sign in front of the database extension related to 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 connects to mysql database
new PDO("mysql:host=localhost;dbname=db_demo","root ","");
The default is not a long connection. If you want to use a long connection to the database, 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 methods and their applications
PDO::query() is mainly used for records Operations that return results, especially SELECT operations
PDO::exec() are mainly for operations that do not return a result set, such as INSERT, UPDATE and other operations
PDO::lastInsertId() returns the last insertion operation, primary key The column type is the last auto-incrementing ID
PDOStatement::fetch() is used to get a record
PDOStatement::fetchAll() is used to get all records into one
5 .PDO operates MYSQL database instance