In
PHP Development, MySQL or mysqli is now using more database connection methods. PDO support has been started in PHP5, and it has been heard that in the next version of PHP6, PDO will be the default database link tool, whether true or false, as another way to connect PDO as a database may be very common in the near future, here Qingyuan on the current grasp of knowledge, The use of PDO as an example of the way to share with you, the article part of the code from the network.
Before using PDO, first set the php.ini file so that PHP supports PDO because PHP5 is not supported by default. Locate the php.ini file in the PHP installation directory, open and search for Extension=php_pdo.dll and extension=php_pdo_mysql.dll, remove the preceding semicolon (;), and restart Apache.
Then we started to write the PHP code, the first is the connection
Database, see the following example:
EXEC (' Set CHARACTER set '. Db_charset); $DBH->exec (' SET NAMES '. Db_charset);} catch (Pdoexception $e) { print "error!:". $e->getmessage (). "
"; Die ();}? >
After the connection is successful, we need to make the data in the database to be modified and checked, which mainly uses the exec and query two functions.
Example:
EXEC ($sql _insert); Returns TRUE or Fal$lastinsertid = $DBH->lastinsertid () of bool;//delete Data $sql_delete = ' Delete from wp_options Where option_id= '. $lastInsertId; $back = $DBH->exec ($sql _delete); Returns TRUE or Fal$lastinsertid = $DBH->lastinsertid () for bool,//Update data $sql_update = ' Update wp_options SET option_name = \ ' . Time (). Rand (1,100). ' \ ' Where option_id= '. $lastInsertId; $lastUpdateId = $DBH->lastinsertid (); Returns the corresponding operation of the id//query data $sql_select = ' Select option_id from Wp_options orDER by option_id DESC LIMIT 4 '; $back = $DBH->quer Y ($sql _select); Returns an object that the object can use foreach to iterate through the loop's result set $back = $DBH->query ($sql _select)->fetch (); Returns a data result this object can be directly traversed with a foreach loop for the result set of the query $back = $DBH->query ($sql _select)->fetchall (); Returns an array this object can be used with foreach to iterate through the loop's result set $back = $DBH->query ($sql _select)->fetchcolumn (0); Returns a field string that is the first field of the first record of the returned record?>
Here, the basic operation of PDO is basically over. For the General Enterprise website, small management system basically can meet, but if we develop large-scale web site or logic more complex management system, only by the data read and write is not enough, we may also need to the database of stored procedures, transactions and other objects to operate, Let's go on to see how PDO is implemented.
For the transaction, the PDO is already encapsulated, and we just need to call it.
Example:
BeginTransaction ();//Open the contents of a transaction//sql execution ... .... ..... $DBH->exec ($sql _insert); Perform a series of operations $DBH->exec ($sql _update); $DBH->exec ($sql _delete); Wait $DBH->commit ();//If execution is done correctly then confirm commit} catch (Exception $e) { $DBH->rollback ();//If there is an error in execution rolling back}?>
It is important to note that when working with transactions, be sure to use try: Catch statement, the transaction is not committed when an error is returned.
PDO still uses the older method when executing the stored procedure, because this piece
QingyuanIt's not quite clear yet. If you have a better way to share it.
Look at the code:
EXEC ($sql _procedure); $sql _call_procedure = ' Call Inout_test (@out_option_name, @out_option_value) '; $DBH->exec ($sql _call_procedure); $sql _select_procedure = ' Select @out_option_name, @out_option_value '; $back = $DBH->query ($sql _select_procedure)->fetch (); $back = $DBH->exec ($sql _drop_procedure);} catch (Exception $e) { echo $e->getmessage ();}? >
In addition, we will introduce a little bit of knowledge about PDO pretreatment in Uncletoo. The preprocessing mechanism has many advantages, he can make the SQL statement reused, and the execution efficiency is higher than the direct execution, so if we use the PDO preprocessing mechanism well, it is very good for the program. (Ps:t good PHP q buckle 峮: 276167802, verify: CSL)
Look at the following example code:
Prepare (' Insert into Wp_options (blog_id,option_name,option_value,autoload) VALUES (0,:option_name,:option_value,\ ' No\ '); $option _name = $option _value = "; $stmt->bindparam (': option_name ', $option _name); $stmt->bindparam (': Option_value ', $option _value); Insert a data A$option_name = ' name '. Time (); $option _value = ' value '. Time (); $stmt->execute (); Insert another different data B$option_name = ' name_ '. Time (); $option _value = ' value_ '. Time (); $stmt->e Xecute (); $stmt = $DBH->prepare (' Insert into Wp_options (blog_id,option_name,option_value,autoload) VALUES (0,?,?, \ ' no\ ') '); $option _name = $option _value = "; $stmt->bindparam (' 1 ', $option _name); $stmt->bindparam (' 2 ', $option _value); Here we can also insert a data $option_name = ' name '. Time (); $option _value = ' value '. Time (); $stmt->execute (); Here we can also insert b data $option_name = ' name_ '. Time (); $option _value = ' value_ '. Time (); $stmt->execute (); $stmt = $DBH ->prepare ("select * from wp_options where option_id =?"); if ($stmt->execute (Array (@$_get[' option_id '))) {while (@ $row = $stmt->fetch ()) {Print_r ($row); }}?>
These are the basis of the PDO, basically can meet our needs in the development process, for other functions, such as PDO big object, error handling, cursors and other functions you can refer to the official PHP manual, here do not explain (in fact, Qingyuan will not ^_^ ... )
http://www.bkjia.com/PHPjc/635048.html www.bkjia.com true http://www.bkjia.com/PHPjc/635048.html techarticle in PHP development, MySQL or mysqli is now used more database connection mode. PDO support has been started in PHP5, and it has been heard that in the next version of PHP6, PDO will be used as the default ...