Home  >  Article  >  Backend Development  >  How to use PDO to connect to the database using jdbc in PHP5

How to use PDO to connect to the database using jdbc in PHP5

WBOY
WBOYOriginal
2016-07-29 08:43:221235browse

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 were a bunch of database extensions to communicate with each database. Connection and processing, what php_mysql.dll, php_pgsql.dll, php_mssql.dll, php_sqlite.dll and so on.
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 ";" in front of "extension=php_pdo.dll". If you want to connect to the database, also You 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 the 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. Common PDO methods and their applications
PDO: :query() is mainly used for operations that return recorded results, especially SELECT operations.
PDO::exec() is mainly used for operations that do not return a result set, such as INSERT, UPDATE and other operations.
PDO::lastInsertId() returns In the last insertion operation, the primary key column type is the last auto-incremented ID.
PDOStatement::fetch() is used to get a record.
PDOStatement::fetchAll() is used to get all the record sets into one.
5.PDO operation 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);
}
?> ;

The above introduces the method of using PDO to connect to the database using jdbc in PHP5, including the content of jdbc connecting to the database. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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