Summary of PHPPDO

Source: Internet
Author: User
This article mainly introduces some understanding of PHPPDO. This article describes what PDO is, how to enable PDO configuration, PDO pre-defined classes, transaction processing examples, and so on, for more information, see

This article mainly introduces some understanding of php pdo. This article describes what PDO is, how to enable PDO configuration, PDO pre-defined classes, transaction processing examples, and so on, for more information, see

1. the PDO (PHP Data Object) Extension defines a lightweight and persistent interface for PHP to access the database. Each database driver implementing the PDO interface can express its own characteristics in the form of regular extension.

Major: PDO extension is only an abstract interface layer. Using PDO extension itself cannot implement any database operations, and a specific database PDO driver must be used to access the database.

2. Start PDO: Find the php. ini file

The Code is as follows:


; Extension = php_pdo.dll


Remove the semicolon (similar to that in linux)

3. PDO predefined classes:

PDO contains three predefined classes: PDO, PDOStatement, and PDOException.

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

PDO: constructor to create a new PDO object

BeginTransaction: Start transaction

Commit: Submit a transaction

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

ErrorInfo: returns an array containing error information from the database, if any

Exec: Execute an SQL statement and return the affected number of rows.

GetAttribute: returns the connection attribute of a database.

LastInsertId: returns the row (ID) that is newly inserted to the database)

Prepare: prepare an SQL statement for execution and return the Union result set after the statement is executed.

Query: executes an SQL statement and returns the result set.

RollBack: Roll back a transaction

SetAttribute: Set a database connection attribute.

(2) PDOStatement class: indicates a preprocessing statement and the Union result set after the statement is executed.

BindColomn: bind a PHP variable to the result gathering Column

BindParam: bind a variable to a parameter in the PHP preprocessing statement.

BindValue: bind a value to a parameter in the processing statement.

CloseCursor: Close the cursor so that the statement can be executed again

CloumnCount: Number of columns in the returned result set

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

ErrorInfo: returns an array containing error information from the statement.

Execute: execute a preprocessing statement.

Fetch: extract a row from the result set

FetchAll: extracts an array containing all rows from the result set.

FetchColomn: data of a column in the returned result set

GetAttribute: returns a PDOStatement attribute.

GetColomnMeta: Structure of a column in the returned result set

NextRowset: returns the next result set.

RowCount: returns the number of rows affected by SQL statement execution.

SetAttribute: Set a PDOStatement attribute.

SetFetchMode: set to get data for PDOStatement

A simple example of transaction processing:

The Code is as follows:


<? Php
/*
Transaction Processing
MYSQL table engine MyISAM InnoDB
Add field alter table user add money int not null default 0;
View table engine show create table user
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 the character set
$ SQL = "set name utf8 ";
$ Pdo-> exec ($ SQL );
// Enable 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 ();
}
?>

(Major Features of transactions: atomicity, consistency, independence, and persistence)

4. The biggest feature of PDO is the introduction of parameter binding and pre-compilation.

Pre-compilation is responsible for two things, and the transfer and soft parsing speed is improved. Programs must support pre-compilation. In addition to database support, drivers must also be supported (PDO and NySQLi support)

5. PDO efficiency problems

(1) The CRUD efficiency of PDO is 5%-lower than that of MySql direct connection in a large table with large data volume ~ 15%, and the variance is greater than MySQL direct connection

(2) In terms of load, after PDO enables persistent connections, the load is higher than MySQL and is relatively stable.

In fact, in actual applications, 90% of the programs do not migrate the database, and 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.