PDO is a mysql database operation function in php of a higher version. Let's take a look at some learning notes about PDO in php. If you are interested in PDO, just take a look. PDO is a mysql database operation function in php of a higher version. Let's take a look at some learning notes about PDO in php. If you are interested in PDO, just take a look.
Script ec (2); script
1. Introduction to PDO
PDO is short for PHP Data Object. It defines the abstract layer for database access and unifies access interfaces of various databases. PDO has the following features:
Encoding consistency: PDO supports multiple database extensions and provides unified access interfaces.
Flexibility: PDO provides unified SQL operation methods and attributes
Efficiency: PDO is compiled in C and PHP.
Object-oriented: PDO fully adopts the object-oriented idea
Databases supported by PDO:
Databases supported by pdo
However, it should be noted that PDO only provides a set of database access interfaces, and PDO itself cannot operate any database.
Ii. PDO object
Use the PDO constructor to connect to a specific database and create a PDO object. Before using PDO, you must enable the PDO extension. Take PHP + MySQL as an example,
Enable PHP extension for PDO: remove the comments of extension = php_pdo.dll from php. ini.
Enable MySQL extension for PDO: remove the comments of extension = php_pdo_mysql.dll from my. ini.
You can use phpinfo () to check whether the PDO extension is enabled.
1. Connect to the database
There are three ways for PDO to connect to a database:
1.1 Connect using parameters (recommended)
$ Dsn = "mysql: host = localhost; dbname = xxx ";
$ Username = "xxxxx ";
$ Pwd = "xxxx ";
$ Pdo = new PDO ($ dsn, $ username, $ pwd );
The PDO constructor also has a $ options parameter, which is an array used to configure the running database, such as whether to enable automatic submission and set the return method of the result set.
1.2 connect through uri
First, create a file to save the data source. The file content is like: mysql: host = localhost; dbname = xxx. Then introduce the file in the program.
$ Dsn = "uri: file: // save the file path of the data source configuration ";
$ Username = "xxxxx ";
$ Pwd = "xxxx ";
$ Pdo = new PDO ($ dsn, $ username, $ pwd );
1.3 connect through the configuration file
Add the data source configuration at any location in php. ini:
Pdo. dsn. test = "mysql: host = localhost; dbname = xxxx ";
Test is the name of the custom data source. For convenience, you can add it in the first line of php. ini, restart the server, and introduce the data source to the program.
$ Dsn = "test"; // data source name
$ Username = "xxxxx ";
$ Pwd = "xxxx ";
$ Pdo = new PDO ($ dsn, $ username, $ pwd );
2. Common methods and attributes of PDO objects
Method or attribute description
Exec () executes an SQL statement and returns the number of affected rows. Used to add, delete, modify, and return 0 to the query.
Query () executes an SQL statement and returns the PDOStatement object, which is used to save the result set.
Prepare () executes an SQL statement and returns the PDOStatement object.
Quote () returns a string that contains quotation marks for SQL statements to prevent SQL injection.
LastInsertId returns the last inserted ID
SetAttribute () sets database connection attributes
GetAttribute () Get database connection attributes
ErrorCode () gets the SQLSTATE related to the last operation of the database handle
ErrorInfo () gets error information related to the last operation of the database handle
BeginTransaction () starts a transaction. Disable Automatic submission first
Commit a transaction
RollBack () rollBack transaction
InTransaction () check whether the operation is in the transaction
3. PDOStatement object
The query () and prepare () Methods of the PDO object return a PDOStatement object, which can be used for PDO preprocessing. The common method is as follows:
Method description
Execute () to execute a preprocessing statement
RowCount () returns the number of rows affected by the previous SQL statement.
Fetch () gets a row from the result set
FetchAll () returns an array containing all rows in the result.
SetFetchMode () sets the default mode for obtaining result sets.
FetchObject () obtains the next row of the result set and returns the result as an object.
FetchColumn () gets the unique column of the next row in the result set.
BindParam () binds a parameter to the specified variable name.
BindValue () binds a value to a specified parameter.
BindColumn () bind a column to the PHP variable
GetColumnMeta () returns the element data of a column in the result set.
ColumnCount () returns the number of columns in the result.
SetAttribute () sets a statement attribute
GetAttribute () gets a statement attribute
ErrorCode () gets the SQLSTATE related to the last operation of the database handle
ErrorInfo () gets error information related to the last operation of the database handle
DebugDumpparams () Prints an SQL preprocessing statement
NextRowset () is pushed to the next row set in a multi-row set statement handle.
Iv. Exception Mode
PDO supports three exception modes:
Default Mode: PDO: ERRMODE_CLIENT
Warning mode: PDO: ERRMODE_WARNING
Exception mode: PDO: ERRMODE_EXCEPTION (recommended)
You can set it in the fourth $ options parameter of the PDO constructor or using the setAttribute () method of the PDO object.