Home > Backend Development > PHP Tutorial > Summary of some understanding of PHP PDO, summary of phppdo_PHP tutorial

Summary of some understanding of PHP PDO, summary of phppdo_PHP tutorial

WBOY
Release: 2016-07-13 10:09:17
Original
753 people have browsed it

Summary of some understanding of PHP PDO, summary of phppdo

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

Copy code The code is as follows:

;extension=php_pdo.dll

Just remove the preceding semicolon (similar in Linux environment)

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:

Copy code The code is as follows:

/*
Transaction processing
        MYSQL table engine MyISAM InnoDB
           Add fields alter table user add money int not null default 0;
View table engine show create table user
Modify table engine alter table user engine=InnoDB
*/

       
Try{
​​​​ //Instantiate PDO
          $pdo=new PDO("mysql:host=localhost;dbname=photo","root","123456".array('3'=>'2'));
}catch(PDOException $e){
echo $e->getMessage();
}
//Set character set
$sql="set name utf8";
$pdo->exec($sql);
//Start transaction processing
$pdo->beginTransaction();
$num=250;
$sql="update user set money=money-{$num} where id =1";
$rows=$pdo->exec($sql);
       
$sql="update user set monet=money-{$num} where id=2";
$rows+=$pdo->exec($sql);
//End transaction processing
If($rows==2){
          $pdo->commit();
}else{
          $pdo->rollBack();
}
?>

(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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/946747.htmlTechArticle Summary of some understanding of PHP PDO, phppdo summary 1. PDO (PHP Data Object) extension defines a A lightweight, persistent interface to the database. Every database that implements the PDO interface...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template