PDO base

Why use PDO

  PDO is a major feature PHP5 newly added, our database server MySQL, the database operation all the code is all a mysql () or mysqli () function to operate, when we need to replace such a database be replaced, SQLSERVER , PostgreSQL, MS, etc., we can not go to modify all the code! So we should use PDO, PDO good to help us solve this problem, use PDO operation is very easy, only need to modify the source data format, and load the appropriate drivers to PHP.ini can;

 

PDO is characterized by

  1) coding consistency
       due to various databases available PHP extensions are written by different issuers, so despite all extensions provide substantially the same characteristics, but do not meet the coding consistency. PDO eliminate this inconsistency, it provides a single interface for a variety of databases;
  2) flexibility
       because PDO database driver must be loaded at runtime, it is not necessary to reconfigure and recompile PHP each use different databases . For example, if you need to switch from the SQL database to MySQL, PDO_MYSQL only need to load drivers on it.
  3) object-oriented features
       PDO PHP5 object-oriented characteristics can be obtained more powerful, more efficient database communication.
  4) High-performance
       PDO is written in C and compiled into PHP, compared to other solutions written in PHP, though others are the same, but offers higher performance.

 

PDO installation

  By PHP's phpinfo () function to see if the PDO extension is installed.

  Add the extension: extension = pdo.so

 

Basic use

  Password (1) connect to the database, the database user name, database
       (2) subject to PDO
       (3) execute the query
 
? < PHP 
$ the DBMS = ' MySQL ' ;      // database type 
$ Host = ' localhost ' ; // database hostname 
$ dbName = ' the Test ' ;     // database using 
$ the User = ' root ' ;       // Database user name 
$ Pass = '' ;           // password corresponding 
$ DSN = " $ DBMS: = $ Host Host; dbName dbname = $ " ; 


the try { 
    $ DBH = new new the PDO ($ DSN, User $, $ Pass);// initialize a PDO object 
    echo " successful connection a " ;
     / * You can also perform a search operation 
    the foreach ($ dbh-> Query ( '* from the SELECT FOO') AS $ Row) { 
        print_r ($ Row) ; // you can use the echo ($ GLOBAL); these values to see 
    } 
    * / 
    $ DBH = null ; 
} the catch (PDOException $ E) { 
    Die ( " Error !: " . $ E-> the getMessage (). " a " ); 
} 
// default this is not a long connection, if the connection requires a database long, finally add a required parameter: array (PDO :: ATTR_PERSISTENT => true) becomes so: $ DB = new new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true));
?>

 

Guess you like

Origin www.cnblogs.com/starfish29/p/10942367.html