PDO study notes

Source: Internet
Author: User
: This article mainly introduces the PDO study notes. if you are interested in the PHP Tutorial, refer to it. The PDO extension defines a lightweight and consistent interface for PHP to access the database. It provides a data access abstraction layer so that no matter what database is used, you can use consistent functions to query and obtain data.
PDO is released with PHP5.1 and can also be used in the PECL extension of PHP5.0, and cannot run in the previous PHP version.

In the case of PDO, the function connecting to the database in PHP varies depending on the database.
For example, MySQL uses the mysql_connect function and PostgreSQL database uses the pg_connect function.

The code written through PDO will change the database in the future. you only need to modify the database connection parameters as appropriate, without modifying the logic code.

PDO and drivers of major databases are released as extensions together with PHP. to activate them, simply edit the php. ini file:
extension=php_pdo.dll
Then, select to use dl () to load the DLL files for a specific database at runtime, or enable them after the php_pdo.dll lines in the php. ini file, such:

extension=php_pdo.dllextension=php_pdo_mysql.dllextension=php_pdo_pgsql.dllextension=php_pdo_sqlite.dll
MySQL database connection
$dsn = 'mysql:dbname=yii2test;host=localhost';$user = 'sqluser';$password = 'sqlpassword';$db = new PDO($dsn, $user, $password);try{    $dbh = new PDO($dsn, $user, $password);}catch (PDOException $e){    print('Error:'.$e->getMessage());    die();}
PostgreSQL database connection
$dsn = 'pgsql:dbname=yii2test host=localhost port=5432';$user = 'sqluser';$password = 'sqlpassword';try{    $dbh = new PDO($dsn, $user, $password);}catch (PDOException $e){    print('Error:'.$e->getMessage());    die();}
SQLite database connection
$dsn = 'sqlite:d:/sqlite/yii2test.db';$user = '';$password = '';try{    $dbh = new PDO($dsn, $user, $password);}catch (PDOException $e){    print('Error:'.$e->getMessage());    die();}

Demo code of PDO for MySQL database operation:


  Prepare ($ SQL); $ param = []; $ param [] = 'admin '. date ('ymdhis '); $ param [] = time (); $ param [] = md5 (time (); $ param [] = 10; if ($ stmt-> execute ($ param) {echo "insert OK! ". PHP_EOL;} else {echo" insert ng! ". PHP_EOL;} // echo PHP_EOL. "=> query ". PHP_EOL; $ SQL = "select * from user"; $ data = $ db-> query ($ SQL); foreach ($ dataas $ row) {echo $ row ["username"]. "". $ row ["password"]. PHP_EOL;} // echo PHP_EOL. "==> prepare 1 ". PHP_EOL; $ SQL = 'select * from user where username like? '; $ Stmt = $ db-> prepare ($ SQL); $ stmt-> execute (['admin %']); while ($ result = $ stmt-> fetch (PDO: FETCH_ASSOC) {print_r ($ result);} // echo PHP_EOL. "==> prepare 2 ". PHP_EOL; $ SQL = 'select * from user where username like: username'; $ stmt = $ db-> prepare ($ SQL); $ stmt-> execute ([': username '=> 'admin %']); while ($ result = $ stmt-> fetch (PDO: FETCH_ASSOC) {print_r ($ result );}} catch (PDOException $ e) {print ('Error :'. $ e-> getMessage (); die () ;}// close the database connection $ db = null;

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above introduces the PDO study notes, including some content, and hope to be helpful to friends who are interested in the PHP Tutorial.

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.