1. The PDO (PHP Data Object) extension defines a lightweight, persistent interface for PHP to access the database. Each database driver that implements the PDO interface can express its own characteristics in the form of regular expansion.
Mainly: PDO extension is just an abstract interface layer. The PDO extension itself cannot implement any database operations. A specific database PDO driver must be used to access the database
2. Start PDO method : Find the php.ini file
3. PDO predefined class:
PDO contains three predefined classes: PDO, PDOStatement, PDOException
(1) PDO class: represents a connection between PHP and database
PDO: Constructor, creates a new PDO object
beginTransaction:Start transaction
Commit: Submit transaction
errorCode: Return an error code from the database, if any
errorInfo: Returns an array containing error information from the database, if any
Exec: Execute a SQL statement and return the number of affected rows
GetAttribute: Returns a database connection attribute
LastInsertId: Returns the latest row (ID) inserted into the database
Prepare: Prepare a SQL statement for execution and return the joint result set after the statement is executed
Query: execute a SQL statement and return a result set
rollBack: roll back a transaction
SetAttribute: Set a database connection attribute
(2) PDOStatement class: represents a preprocessing statement and the joint result set after the statement is executed
bindColomn: Bind a PHP variable to the result set output column
bindParam: Bind a variable to a parameter in a PHP prepared statement
bindValue: Bind a value to the parameter in the processing statement
closeCursor: Close the cursor so that the statement can be executed again
cloumnCount: Returns the number of columns in the result set
ErrorCode: Return an error code from the statement, if any
errorInfo: Returns an array containing error information from the statement
execute: execute a prepared statement
fetch: Fetch a row from the result set
fetchAll: Get an array containing all rows from the result set
fetchColomn: Returns the data of a certain column in the result set
GetAttribute: Returns a PDOStatement attribute
GetColomnMeta: Returns the structure of a column in the result set
NextRowset: Returns the next result set
RowCount: Returns the number of rows affected after the SQL statement is executed
SetAttribute: Set a PDOStatement attribute
SetFetchMode: Set to get data for PDOStatement
Give a simple example of transaction processing:
(Main characteristics of transactions: atomicity, consistency, independence and durability)
4. The biggest feature of PDO is the introduction of parameter binding and pre-compilation
Pre-compilation is responsible for two things, transfer and soft parsing speed. To support pre-compilation, in addition to database support, the program also needs driver support (PDO and NySQLi support)
5. PDO efficiency issues
(1) Tested on a large table with large data volume, PDO’s CRUD efficiency is 5%~15% lower than MySql direct connection, and the variance is greater than MySQL direct connection
(2) As for the load, after PDO opens a long connection, the load is higher than MySQL and relatively stable.
In fact, in actual applications, 90% of programs do not perform database migration, and there are very few applications that do database migration.