How to connect a database using PDO in the JDBC Connection database PHP5

Source: Internet
Author: User
1.PDO Introduction
PDO (PHP Data Object) is something added to PHP 5, which is a significant feature of PHP 5, since php4/php3 before PHP 5 is a bunch of database extensions to connect and process with each database, what Php_mysql.dll, Php_ Pgsql.dll, Php_mssql.dll, Php_sqlite.dll and so on.
The PHP6 will also be connected by default using PDO, and the MySQL extension will be used as a secondary
2.PDO Configuration
PHP.ini, remove the "Extension=php_pdo.dll" in front of the ";" To connect to the database, you also need to remove the ";" in front of the database extension associated with 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 connecting MySQL Database
New PDO ("Mysql:host=localhost;dbname=db_demo", "Root", "");
The default is not a long connection, to use a long database connection, 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 method and its application
Pdo::query () is primarily used for operations that have logged results returned, especially the select operation
Pdo::exec () is primarily for operations returned without a result set, such as INSERT, update, and so on
Pdo::lastinsertid () returns the last insert operation, the primary key column type is the final self-increment ID
Pdostatement::fetch () is used to get a record
Pdostatement::fetchall () is to get all recordsets into one
5.PDO operating MySQL DB instance

Copy the Code code 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 "Insert success! ";
Echo $pdo-Lastinsertid ();
}
?>


Copy the Code code as follows:


$pdo = new PDO ("Mysql:host=localhost;dbname=db_demo", "Root", "");
$rs = Query ("SELECT * from Test"), $pdo
while ($row = Fetch () $rs) {
Print_r ($row);
}
?>

The above describes the JDBC Connection database PHP5 using PDO to connect to the database, including the JDBC Connection database content, I hope to be interested in PHP tutorial friends helpful.

  • 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.