PHP Understanding PDO Database Abstraction Layer

Source: Internet
Author: User
Tags dsn getmessage

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<?PHP2 $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=NewPDO ($dsn,$user,$pwd);Ten     Echo' Connected successfully! ‘; One}Catch(Exception $e) { A     Echo $e->getmessage (). "<br>"; - } -?>

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 result sets
the method to get the result set is 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;
The Fetchcolumn () method gets the value of the column specified by the next row in the result set.
the invocation object type of three methods is the Pdostatement object type, which is typically executed after the 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 hints are as follows:
1, Pdo::errmode_silent
by default, the program resumes 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
The error does not continue to occur, and the error section prompts for the 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),error messages can be output in the background via 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.

PHP Understanding PDO Database Abstraction Layer

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.