PDO (PHPDataObject) is a new feature added to PHP5, in the past, php4php3 in PHP5 was 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.
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
The code is 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 "inserted successfully! ";
Echo $ pdo-> lastinsertid ();
}
?>
The code is as follows:
$ Pdo = new PDO ("mysql: host = localhost; dbname = db_demo", "root ","");
$ Rs = $ pdo-> query ("select * from test ");
While ($ row = $ rs-> fetch ()){
Print_r ($ row );
}
?>