PHP recognizes the PDO database abstraction layer and phppdo database abstraction

Source: Internet
Author: User

PHP recognizes the PDO database abstraction layer and phppdo database abstraction

The full name of PDO is PHP Data Object (PHP Data Object). It is an extension of PHP connection to the database and is widely used. PDO provides a unified data access interface and operation layer for different databases. It provides a good solution for implementing cross-database platform development and migration. Obtain a PDO object
In PDO, to establish a connection with the database, you need to instantiate the PDO constructor. The syntax of the PDO constructor is as follows:
PDO: :__ construct (string $ dsn [, string $ username [, string $ password [, array $ driver_options])
Use the PDO constructor to connect to the database:

1 <? Php 2 $ dbms = 'mysql'; 3 $ dbName = 'dormitory '; 4 $ host = 'localhost'; 5 $ user = 'root'; 6 $ pwd = ''; 7 $ dsn = "$ dbms: host = $ host; dbname = $ dbName"; 8 try {9 $ pdo = new PDO ($ dsn, $ user, $ pwd ); 10 echo 'Connection successful! '; 11} catch (Exception $ e) {12 echo $ e-> getMessage (). "<br>"; 13} 14?>

Three methods to execute SQL statements
SQL statement execution methods include exec (), query (), prepare () + execute ().
1. the exec () method is used to execute input, delete, and update statements. The returned value is the number of affected rows;
2. the query () method is used to execute the select statement. The returned value is a two-digit array;
3. The pre-processing statement prepare () + execute () can be used to execute input, delete, update, and select statements,
The pre-processing statement can be considered as a compiled template for the SQL statement to be run. It can be customized using variable parameters so that it can be executed multiple times only once for query;
If the application only uses preprocessing statements, it can ensure that SQL injection is not performed.
PDO: The returned value of prepare () is a PDOStatement object. The object content is a parameter in the prepare () method;
PDO: execute (), check whether the SQL statement is executable, and return a boolean type.
From the returned values of the pre-processing statement, we can see that it does not actually execute the SQL statement, but checks the correctness of the SQL statement, prepares the execution status, and then executes the statement when the result set is needed.

1 <? Php 2 shortde_once '. /pdo_db_conn.php '; 3 try {4 $ query = "select * from building"; 5 // $ result = $ pdo-> query ($ query ); // The result is a two-dimensional array 6 $ result = $ pdo-> prepare ($ query); 7 var_dump ($ result); 8 $ flag = $ result-> execute (); 9 var_dump ($ flag); 10} catch (Exception $ e) {11 echo $ e-> getMessage (); 12} 13?>

Return Value:

Object (PDOStatement) [2]
Public 'querystring' => string 'select * from building '(length = 22)

Boolean true

Three methods to obtain the result set
Methods for obtaining result sets include fetch (), fetchAll (), and fetchColumn ();
The next row in the result set obtained by the fetch () method is an array;
The fetchAll () method is used to obtain all rows in the result set, which is an array;
The fetchColumn () method gets the value of the specified column in the next row of the result set.
The call object type of the three methods is PDOStatement object type, which is generally executed after the pre-processing statement.

Three methods to capture errors in SQL statements
If an error occurs when an SQL statement is executed, you can determine the method for displaying the Error Based on the set error prompt.
The error message is as follows:
1. PDO: ERRMODE_SILENT
By default, the program continues to run when an error occurs. No error message is displayed.
2. PDO: ERRMODE_WARNING
If an error occurs, the execution continues. If the error occurs, a warning message is displayed.
3. PDO: ERRMODE_EXCEPTION
The execution will not continue when an error occurs, and the error part will prompt the exception information.

Two methods to get program error information
If an error occurs when an SQL statement is executed (the result set traversal error is not prompted when accessing the database), you can use errorCode () and errorInfo () to output error information in the background, these two methods are not applicable when SQL statements are executed through pre-compilation. When the errorCode () method is called in the program and the return value is 00000 (5 0), it indicates that the program has no error, but the other five characters are returned, indicates that the program has an error. You can call the errorInfo () method to view the specific error information.


PDO module in PHP

This is slightly related to the PHP version. For example, to enable php_pdo.dll and php_pdo_mysql.dll in PHP5.2, you only need to enable php_pdo_mysql.dll in php5.4.

How does php pdo operate databases?

$ S_ SQL = select username, password from t_table where username =? And password = ?;
$ Something = $ dbh-> prepare ($ s_ SQL );
$ Result = $ something-> execute (array ($ username, $ password ));
$ Result = $……-> fetchAll (); // if no empty array is returned, username + password is returned.

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.