1. About PDO
PDO (PHP Data Object) is a new feature added to PHP 5, because php4/php3 before PHP 5 were a bunch of database extensions to connect to and process various databases, such as php_mysql.dll, php_pgsql.dll, php_mssql.dll, and php_sqlite.dll.
PHP6 will also use the PDO connection by default, and mysql extension will be used as an auxiliary
2. PDO Configuration
PHP. in ini, remove the ";" sign before "extension = php_pdo.dll"; ". To connect to the database, remove the"; "sign before the PDO-related database extension, 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 connection to mysql database
New PDO ("mysql: host = localhost; dbname = db_demo", "root ","");
The default value is not persistent connection. To use persistent connection, add the following parameters at the end:
New PDO ("mysql: host = localhost; dbname = db_demo", "root", "", "array (PDO: ATTR_PERSISTENT => true )");
4. Common PDO Methods and Their Applications
PDO: query () is mainly used for operations that return records, especially SELECT operations.
PDO: exec () is mainly used for operations that do not return result sets, such as INSERT and UPDATE operations.
PDO: lastInsertId () returns the last insert operation. The primary key column type is the last auto-increment ID of auto-increment.
PDOStatement: fetch () is used to obtain a record.
PDOStatement: fetchAll () is to obtain all the record sets to
5. PDO for MYSQL database instances
Copy codeThe Code is as follows:
<? Php
$ Pdo = new PDO ("mysql: host = localhost; dbname = db_demo", "root ","");
If ($ pdo-> exec ("insert into db_demo (name, content) values ('title', 'content ')")){
Echo "inserted successfully! ";
Echo $ pdo-> lastinsertid ();
}
?>
Copy codeThe Code is as follows:
<? Php
$ Pdo = new PDO ("mysql: host = localhost; dbname = db_demo", "root ","");
$ Rs = $ pdo-> query ("select * from test ");
While ($ row = $ rs-> fetch ()){
Print_r ($ row );
}
?>