PDO Learning Notes

Source: Internet
Author: User
Tags dsn
The PDO extension defines a lightweight, consistent interface for the PHP Access database, which provides a data access abstraction layer so that queries and data can be executed through a consistent function, regardless of the database used.
PDO is released with PHP5.1 and can be used in the PHP5.0 pecl extension and cannot be run in previous versions of PHP.

In the presence of PDO, the functions that connect to the database in PHP vary depending on the database.
For example, MySQL uses the Mysql_connect function, the PostgreSQL database with the Pg_connect function.

The code written by PDO, the database changes in the future, only need to properly modify the database connection parameters, no need to modify the logic code.

The drivers for PDO and the main database are released as extensions with PHP, and to activate them simply edit the php.ini file:
extension=php_pdo.dll
Then, select the DLL files for the specific database using the DL () to load at run time, or enable them after php_pdo.dll the lines in the php.ini file, such as:

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';$dbnew PDO($dsn$user$password);try{    $dbhnew PDO($dsn$user$password);}catch$e){    print('Error:'.$e->getMessage());    die();}

PostgreSQL database Connection

$dsn'pgsql:dbname=yii2test host=localhost port=5432';$user'sqluser';$password'sqlpassword';try{    $dbhnew PDO($dsn$user$password);}catch$e){    print('Error:'.$e->getMessage());    die();}

SQLite database Connection

$dsn'sqlite:d:/sqlite/yii2test.db';$user'';$password'';try{    $dbhnew PDO($dsn$user$password);}catch$e){    print('Error:'.$e->getMessage());    die();}

PDO operation MySQL Database demo code:


    $dsn=' mysql:dbname=yii2test;host=192.168.0.69;post=3306 ';$user=' Shou ';$password=' Shouadmin ';Try{$db=NewPDO ($dsn,$user,$password);// $sql=' INSERT into user (username, password,password_hash, status) value (?,?,?,?) ';$stmt=$db->prepare ($sql);$param= [];$param[] =' admin '. Date' Ymdhis ');$param[] = time ();$param[] = MD5 (time ());$param[] =Ten;if($stmt->execute ($param)) {Echo"Insert ok!".    Php_eol; }Else{Echo"Insert ng!".    Php_eol; }// EchoPhp_eol."==> Query". Php_eol;$sql="SELECT * from User";$data=$db->query ($sql);foreach($data as$row) {Echo$row["username"] ."  ".$row["Password"].    Php_eol; }// EchoPhp_eol."==> prepare 1". Php_eol;$sql=' SELECT * from user where username? ';$stmt=$db->prepare ($sql);$stmt->execute ([' admin% ']); while($result=$stmt->fetch (PDO::FETCH_ASSOC)) {Print_r ($result); }// EchoPhp_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 database connection$db=NULL;

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above describes the PDO learning notes, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.