PHP Understanding PDO Database Abstraction Layer, PHPPDO database Abstraction _php Tutorial

Source: Internet
Author: User
Tags dsn

PHP recognizes the PDO database abstraction layer, PHPPDO database abstraction


The PDO full name is a PHP data object, which is an extension of the PHP connection database and is now widely used. PDO mainly solves the problem of providing a unified data access interface and operational layer for different databases. In order to realize the system in the cross-database platform development and migration and other issues to provide a better solution. getting the PDO object
In PDO, the constructor to instantiate the PDO is required to establish a connection to the database. The PDO constructor syntax is as follows:
Pdo::__construct (String $dsn [, String $username [, String $password [, array $driver _options]])
Connect to the database via the PDO constructor:

  1 !--? 
  
    php 
    2  
    $dbms  = ' mysql ' 
   ;  
    3  
    $dbName  = ' dormitory ' 
   ;  
    4  
    $host  = ' localhost ' 
   ;  
    5  
    $user  = ' root ' 
   ;  
    6  
    $pwd  = ' 
    ';  
    7  
    $dsn  = "
    $dbms : host= 
    $host ;d bname= 
    $dbName " 
    ; 
    8  
    try  
    { 
    9  
    $pdo  = 
    new  PDO (
    $dsn ,  $user ,  $pwd  );   ten   echo  ' connection succeeded! ' ;   one }  catch  ( Exception   $e  ) {  |   echo   $e ->getmessage (). "
" ; } + ;

3 Methods of executing SQL statements
The methods for executing the SQL statement are: EXEC (), query (), prepare () +execute ().
1, the Exec () method is used to execute the Input,delete,update statement, the return value is the number of rows affected;
2. The query () method is used to execute the SELECT statement, and the return value is a two-bit array;
3, preprocessing statement prepare () +execute (), can be used to execute the Input,delete,update,select statement,
you can think of a preprocessing statement as a compiled template for the SQL you want to run . He can use variable parameters to customize, so that the query can only parse once to execute multiple times;
If your application uses only preprocessing statements, you can ensure that SQL injection does not occur.
PDO::p repare () returns a Pdostatement object with the contents of the object as a parameter inside the prepare () method;
Pdo::execute (), checks if SQL is executable and returns a Boolean type.
As can be seen from the return value of the preprocessing statement, he does not actually execute the SQL statement, but instead checks the correctness of the SQL, prepares the execution state, and executes when the result set needs to be used.

1 
 Php2             include_once'./pdo_db_conn.php ';3             Try {4                 $query= "SELECT * FROM Building";5 //$result = $pdo->query ($query);//result is a two-dimensional array6                 $result=$pdo->prepare ($query);7                 Var_dump($result);8                 $flag=$result-execute ();9                 Var_dump($flag);Ten}Catch(Exception $e) { One                 Echo $e-getMessage (); A             } -?>

return value:

Object (Pdostatement) [2]
Public ' queryString ' = = String ' select * from building ' (length=22)

Boolean true

3 ways to get a result set
Get a result set by Fetch (), Fetchall (), Fetchcolumn (); The
fetch () method gets the next row in the result set, which is an array; the
fetchall () method gets all the rows in the result set, which is an array;
Fetchcolumn The () method gets the value of the column specified by the next row in the result set.
Three methods Call object type Pdostatement object type, typically executed after preprocessing statement

3 ways to capture errors in SQL statements
When an error occurs when executing an SQL statement, you can determine how to display the error, depending on how you set up the error message.
error prompts are:
1, pdo::errmode_silent
By default, the program continues execution with no error prompts when an error occurs.
2, pdo::errmode_warning
continues execution When an error occurs, and the error section prompts for a warning message.
3, pdo::errmode_exception
does not continue execution when an error occurs, and the error section prompts for exception information.

2 ways to Get program error messages
An error occurred while executing the SQL statement (accessing the Database section, not prompting for a traversal error on the result set), The can output error messages in the background through ErrorCode (), ErrorInfo () Two methods, which are not applicable when the SQL statement is executed by precompilation. when the ErrorCode () method is called in a program, the return value is 00000 (5 0), indicating that the program has no errors, but when the other 5 characters are returned, the program is wrong, You can then view the information for the specific error by calling the ErrorInfo () method.


The PDO module in PHP

This is a little bit related to PHP's version. For example, PHP5.2 to open the Php_pdo.dll and Php_pdo_mysql.dll two, php5.4 only need to open the Php_pdo_mysql.dll this one on the line

How PHP PDO operates the database

$s _sql = select Username,password from t_table where username=? and password=?;
$sth = $dbh->prepare ($s _sql);
$result = $sth->execute (Array ($username, $password));
$result = $sth->fetchall ();//If there is no empty array returned, if there is a user name + password

http://www.bkjia.com/PHPjc/892526.html www.bkjia.com true http://www.bkjia.com/PHPjc/892526.html techarticle PHP know the PDO database abstraction layer, PHPPDO database abstract PDO full name is PHP data Object (PHP), is a PHP connection database extension, is now widely used. PDO Master ...

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