PDO Tips

I. Introduction

PDO (PHP Data Object) provides a common interface to access a variety of databases that abstract data model supports a variety of database connection.

PDO extension for PHP defines a lightweight database access, persistence interface. Which itself does not implement any database operations, you must use a specific database PDO driver to access the database to achieve each database-driven PDO interface can be extended to the regular form of the manifested their own characteristics. It means that regardless of which database you use, you can operate the database using the same set of API.

In PHP, MySQL links in several ways:

  • MySQL family of functions (PHP 7 deprecated)
  • MySQLi series function, is an enhanced version of the above, it adds new features such as pre-compiled and parameter binding
  • PDO

 

Two, PDO predefined classes

See: https://www.php.net/manual/zh/book.pdo.php

PDO、PDOStatement 和 PDOException

PDO class (on behalf of a link between the database and PHP).

PDOStatement class (representing a prepared statement and the statement after the joint execution result sets).

PDOException category (simple rewrite of the Exception of).

 

Three, PDO and pre-compiled and parameter binding

PHP uses prepare API instead of using the default parameter binding and pre-compiled, mainly on account of maximum compatibility issues, because some databases do not support pre-compiled function, so use the default SQL stitching to simulate pre-compiled (transfer, single quotation marks).

If you are using a real pre-compiled ($ pdo-> setAttribute (PDO :: ATTR_EMULATE_PERPARES, false); // use the local prepared statements, rather than analog), the client needs to send in two: 1, SQL templates; 2, SQL query parameters , which is more than a network request overhead, but does not affect the performance, because the pre-compilation process occurs only at the first request, once the compilation is successful, the second can be used directly, and does not require secondary compilation (the same Session, that is, each time the request of multiple calls there is only one pre-compiled).

Pre-compiled MySQL support, but is still relatively weak, only supports Session level, PHP's every request is a new session, so each request will inevitably need to re-do a pre-compiled. But if you use connection pooling, it can make multiple requests to use the same data connection PHP Session, so as to improve the performance of pre-compiled.

 

Four, PDO transaction

At the end of the script, or a connection to be closed, if there is a complete unprocessed transaction, PDO will automatically roll it back. This is a security program for the unexpected termination of the script is. If you do not explicitly commit the transaction, it is assumed that something went wrong, for the security of the data rollback.

 

Five, PDO efficiency

From third-party testing:

PHP5.3,60 plurality of tables, 2GB of data, the efficiency of PDO CURD direct MySQL lower than 5% to 15%.

After a long turn on the connected load is higher than the PDO MySQL direct and relatively stable, the connection speed is also an advantage.

Practical applications, 90% of the program is not for database migration, it seems PDO necessity is not large.

In summary: recommended for new applications try to use the PDO, old application is not necessary to be reconstructed. In fact, the current mainstream basic framework underlying PDO is achieved.

 

Guess you like

Origin www.cnblogs.com/cshaptx4869/p/11575526.html