Some understanding of PHP PDO _php skills

Source: Internet
Author: User

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 display its own features in the form of regular expansions.

Main: PDO extension is only an abstract interface layer, the use of PDO extension itself can not achieve any database operations, must use a specific database PDO drive access to the database

2, Start PDO method : Find php.ini file will

Copy Code code as follows:

; Extension=php_pdo.dll

Before the semicolon can be removed (similar in the Linux environment)

3, PDO predefined classes:

The PDO contains three predefined classes: PDO, Pdostatement, pdoexception

(1) PDO class: Represents a connection between PHP and the database

PDO: Builder, creating a new PDO object

BeginTransaction: Starting a transaction

Commit: Commit a transaction

ErrorCode: Return an error code from the database, if any

ErrorInfo: Returns an array containing error messages from the database, if any

EXEC: Executes an SQL statement and returns the number of rows affected

GetAttribute: Returns the connection properties of a database

Lastinsertid: Returns the most recent row inserted into the database (ID)

Prepare: Prepares a SQL statement for execution that returns a union result set after the statement is executed

Query: Executes an SQL statement and returns the result set

RollBack: Rolling back a transaction

SetAttribute: Set a database Connection property

(2) Pdostatement class: Represents a preprocessing statement and a joint result set after statement execution

Bindcolomn: Bind a PHP variable to the result set output column

Bindparam: Binding a variable to a parameter in a PHP preprocessing statement

Bindvalue: Binding a value to a parameter in a processing statement

Closecursor: Closes the cursor so that the statement can be executed again

Cloumncount: Returns the number of columns in the result set

ErrorCode: Returns an error code from the statement, if any

ErrorInfo: Returns an array containing error messages from a statement

Execute: Executes a preprocessing statement

Fetch: Fetching a row from the result set

Fetchall: An array containing all rows is fetched from the result set

Fetchcolomn: Returns data from a column in the result set

GetAttribute: Returns a Pdostatement property

Getcolomnmeta: Returns the structure of a column in a result set

Nextrowset: Returns the next result set

ROWCOUNT: Returns the number of rows affected after the execution of the SQL statement

SetAttribute: Set a Pdostatement property

Setfetchmode: Get Data for pdostatement settings

A simple example of a transaction:

Copy Code code as follows:

<?php
/*
Transaction processing
MYSQL Table Engine MyISAM InnoDB
Add field ALTER TABLE user add Money int is not NULL default 0;
View table engine Show CREATE TABLE user
Modify table engine ALTER TABLE user Engine=innodb
*/


try{
Instantiation of PDO
$pdo =new PDO ("Mysql:host=localhost;dbname=photo", "root", "123456". Array (' 3 ' => ' 2 '));
}catch (Pdoexception $e) {
echo $e->getmessage ();
}
Setting the character set
$sql = "set name UTF8";
$pdo->exec ($sql);
Turn on 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 features of the transaction: atomicity, consistency, independence, and persistence)

4. The most characteristic of PDO is the introduction of parameter binding and precompilation

Precompilation is responsible for two things, shifting and soft parsing speed. Program to support precompilation, in addition to database support, also requires driver support (PDO and NYSQLI support)

5, the PDO efficiency problem

(1) In a large table large data volume test, PDO crud efficiency than MySQL direct connection low 5%~15%, and the variance is greater than the MySQL direct connection

(2) As for the load, the PDO open long connection after the load is higher than MySQL and more stable.

In fact, in the actual application, 90% of the program is not a database migration, there are few database migration applications.

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.