The PDO function library in PHP

Source: Internet
Author: User

PDO is a "database access abstraction Layer" that unifies the access interfaces of various databases, and PDO makes cross-database usage more affinity than MySQL and mysqli libraries, and PDO is more efficient than ADODB and MDB2.

For now, implementing a "database Abstraction Layer" is a long way off, and using a "Database access abstraction Layer" like PDO is a good choice.

The PDO contains three predefined classes

The PDO contains three predefined classes, PDO, Pdostatement, and Pdoexception, respectively.

One, PDO

Pdo->begintransaction ()-Indicates the rollback start point
Pdo->commit ()-Indicates the rollback end point and executes the SQL
Pdo->__construct ()-Create an instance of the PDO link database
Pdo->errorcode ()-Get error code
Pdo->errorinfo ()-Get the wrong information
Pdo->exec ()-Processes an SQL statement and returns the number of entries affected
Pdo->getattribute ()-Gets a property of a "Database connection object"
Pdo->getavailabledrivers ()-Get a valid PDO drive name
Pdo->lastinsertid ()-Gets the primary key value of the last piece of data written
Pdo->prepare ()-Generates a "Query object"
Pdo->query ()-Processes an SQL statement and returns a "Pdostatement"
Pdo->quote ()-adds quotation marks to a string in a SQL
Pdo->rollback ()-Perform rollback
Pdo->setattribute ()-Set properties for a database connection object

Second, pdostatement

Pdostatement->bindcolumn ()-bind a column to a PHP variable
Pdostatement->bindparam ()-binds a parameter to the specified variable name
Pdostatement->bindvalue ()-binds a value to a parameter
Pdostatement->closecursor ()-closes the cursor, enabling the statement to be executed again.
Pdostatement->columncount ()-returns The number of columns in the result set
Pdostatement->errorcode ()-fetch the SQLSTATE associated with the last operation on the statement handle
Pdostatement->errorinfo ()-fetch extended error information associated with the last operation on the statement handle
Pdostatement->execute ()-executes a prepared statement
Pdostatement->fetch ()-fetches the next row from a result set
Pdostatement->fetchall ()-returns An array containing all of the result set rows
Pdostatement->fetchcolumn ()-returns a single column from the next row of a result set
Pdostatement->fetchobject ()-fetches the next row and returns it as an object.
Pdostatement->getattribute ()-retrieve a statement attribute
Pdostatement->getcolumnmeta ()-returns metadata for a column in a result set
Pdostatement->nextrowset ()-advances to the next rowset in a Multi-rowset statement handle
Pdostatement->rowcount ()-returns the number of rows affected by the last SQL statement
Pdostatement->setattribute ()-set a statement attribute
Pdostatement->setfetchmode ()-set The default fetch mode for this statement

PDO is a "database access abstraction Layer" that unifies the access interfaces of various databases, and PDO makes cross-database usage more affinity than MySQL and mysqli libraries, and PDO is more efficient than ADODB and MDB2. For now, implementing a "database Abstraction Layer" is a long way off, and using a "Database access abstraction Layer" like PDO is a good choice.

Third, pdoexception

PDO provides a different error handling strategy in 3.
1. Pdo::errmode_silent
This is the default mode used. PDO will set simple error codes on the statement and database objects, and you can use the Pdo->errorcode () and Pdo->errorinfo () methods to check for errors If the error is caused by a call to the statement object, you can use the Pdostatement->errorcode () or Pdostatement->errorinfo () method on that object to get the error message. If the error is caused by a call to the database object, you should call the two methods on the database object.
2. pdo::errmode_warning
PDO will emit a traditional e_warning message as an addition to the setup error code. This setting is useful in debugging and debug, if you just want to see what's going on and don't want to interrupt the process.
3. Pdo::errmode_exception
As an attachment that sets the error code, PDO throws an Pdoexception exception and sets its properties to reflect the error code and error message. This setting is useful in addition to errors, because he will effectively "zoom in (Blow up)" The error point in the script, very quickly point to a possible error area in your code. (Remember: If the exception causes the script to break, the transaction is automatically rolled back.) )
The exception mode is also useful because you can use a much clearer structure-handling error than the traditional PHP-style error-handling structure, using less code and nesting than quiet mode, and more explicitly checking the return value of each database access.
For more information about exceptions in PHP, see the Exceptions section
PDO uses the SQL-92 SQLSTATE-based error code string, and the specific PDO driver should correspond its own code name to the appropriate SQLSTATE code. The Pdo->errorcode () method returns only a single SQLState designator. If you need more targeted information about a bug, PDO also provides a pdo->errorinfo () method that returns a string containing the SQLSTATE code, a specific database-driven error ID, and a specific database-driven error description.

The PDO function library in PHP

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.