The PDO contains three predefined categories: PDO, Podstatement, and Pdoexception.
1.PDO class
The PDO class represents a connection between PHP and a database, and the PDO class has the following methods:
PDO: constructor to build a new PDO object.
BeginTransaction: Transaction begins.
Commit: Commit a transaction
ErrorCode: Returns an error code from the database.
ErrorInfo: Returns a data containing error information from the database.
EXEC: Executes an SQL statement and returns the number of rows affected
GetAttribute: Returns a database Connection property.
Lastinsertid: Returns the last row ID inserted into the database
Prepare: Prepares an SQL statement for execution and returns the union result set (pdostatement) after the statement executes.
Query: Executes an SQL statement and returns the result set
Quote: Returns a string that adds quotation marks so that it can be used in SQL
RollBack: Transaction rollback
SetAttribute: Setting a Database Connection property
2.PDOStatement class
The Pdostatement class represents a preprocessing statement and a collection of union results after the statement executes, with the method:
Bindcolumn: Binds a PHP variable to the output sequence in the result set.
Bindparam: Binding a PHP variable to a parameter in a preprocessing statement
Bindvalue: Binding a value to a parameter in a preprocessing statement
Closecursor: Closes the cursor, which is the statement that can be executed again.
ColumnCount: Returns the number of columns in the result set
ErrorCode: Returns an incorrect code name from the statement
ErrorInfo: Returns an array containing error information from the statement
Execute: Executes a preprocessing statement
Fetch: Remove a row from the result set
Fetchall: Remove an array containing all rows from the result set
Fetchcolumn: Returns the data in a column in the result set.
GetAttribute: Returns a Pdostatement property
Getcolumnmeta: Returns the structure of a column in the result set.
Nextrowset: Returns the next result set
RowCount: Returns the number of rows affected by the SQL statement execution
SetAttribute: Setting a Pdostatement property
Setfetchmodel: Sets the way to get data for pdostatement.
3.PDOException
Pdoexception is simple from writing to exception.
Using PDO Case 1:
<?PHPTry{$DSN='Mysql:host=localhost;dbname=shen';//set up a data source for PDO$db =NewPDO ($DSN,'Root','Rootroot');//Construction Method//Setting Exceptions$dbSetAttribute (pdo::attr_errmode,pdo::errmode_exception); $db->exec ("Set names UTF8"); $sql="INSERT into A2 (name) values (' asdf ')"; $db->exec ($sql);//EXECUTE Statement//using preprocessing$insert = $db->prepare ('INSERT into A2 (name) values (?)'); $insert->execute (Array ('Wang')); $insert->execute (Array ('Lisi'));
Query Results $sql='select name from A2'; $query= $dbprepare ($sql); $query-execute (); Var_dump ($query-Fetchall (PDO::FETCH_ASSOC)); }Catch(pdoexception $err) {echo $err-getMessage ();}?>
Case 2: Binding parameters and Precompilation
<? PHP Try { $dsn='mysql:host=localhost;dbname=shen'; Set the data source for PDO $dbh= new PDO ($DSN,'root',' Rootroot '); //
$DBH->setattribute (pdo::attr_errmode,pdo::errmode_exception);
$name ='Wang'; $sth= $dbh->prepare ('select name from A2 where name =: Name'); $sth->bindparam (': Name', $name); $sth-execute (); Var_dump ($sth-Fetchall (PDO::FETCH_ASSOC)); $sth= $dbh->prepare ('SELECT * from a2 where name =?'); $sth->bindparam (1, $name, PDO::P aram_str); $sth-execute (); Var_dump ($sth-Fetchall (PDO::FETCH_ASSOC)); }Catch(pdoexception $err) {echo $err-getMessage ();}?>
In the MySQL application, in order to prevent the total injection, it is common to use functions such as intval,addslashes to escape the value into a legitimate type in SQL, this method is more complex, and the Bindparam method using PDO becomes relatively simple, Just specify the third parameter in the function, you can convert the value to the desired type and stitch it into the original statement.
Case 3: Transaction processing
Transactions generally fall into three steps
1. Start a transaction
2. Executing SQL statements in a transaction
3. Commit the transaction to complete the transaction.
<?PHPTry{$DSN='Mysql:host=localhost;dbname=shen';//set up a data source for PDO$db =NewPDO ($DSN,'Root','Rootroot');//Construction Method$dbSetAttribute (pdo::attr_errmode,pdo::errmode_exception); $db->begintransaction ();//Start a transaction//start execution of SQL statements$db->commit ();//COMMIT TRANSACTION End }Catch(pdoexception $err) {echo $err-getMessage ();}?>
PHP's PDO