How to connect to a database using PDO in PHP5

Source: Internet
Author: User
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.

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, what

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, Hong Kong server, 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 with records returned, U.S. space, especially SELECT operations.
PDO: exec () is mainly used for operations that do not return result sets, such as INSERT and UPDATE operations in the U.S. space.
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 );
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.