PHP recognizes the PDO database abstraction layer and phppdo database abstraction. PHP recognizes the PDO database abstraction layer. the phppdo database abstraction PDO is called PHPDataObject (PHP Data object). It is an extension of PHP connection to the database and is currently widely used. PDO master PHP recognizes the PDO database abstraction layer and phppdo database abstraction layer
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
GetMessage ()."
"; 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
Query ($ query); // The result is a two-dimensional array of 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.
Http://www.bkjia.com/PHPjc/892526.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/892526.htmlTechArticlePHP understand the PDO database abstraction layer, phppdo database abstraction PDO full name is PHP Data Object (PHP Data Object), is an extension of PHP connection database, is currently widely used. PDO master...