How to use PDO to connect to the database in PHP5_PHP tutorial

WBOY
Release: 2016-07-21 15:36:21
Original
1077 people have browsed it

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

Copy code 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 "Insertion successful! ";
echo $pdo -> lastinsertid();
}
?>


Copy code 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);
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322170.htmlTechArticle1. Introduction to PDO PDO (PHP Data Object) is something added in PHP 5 and is new to PHP 5 A major feature, because before PHP 5, php4/php3 had a bunch of database extensions to keep track of each data...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template