PDO (20161107)

Source: Internet
Author: User
Tags dsn

Pdo

Mysqli is for MySQL database extension, is specifically to access the MySQL database

PDO is an extended class of database access that accesses multiple databases through a single class, which is a data access abstraction layer that combines classes

can access other databases, which is the abstraction layer of data access

<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >PHP//PDO//1. Access to different database//2. The transaction function can be restored by a rollback operation. To prevent SQL injection//pdo from being used ./*//1. Object $dsn = "Mysql:dbname=mydb;host=localhost"; " Driver Name: Database name, server address "$pdo = new PDO ($DSN," root "," 123 "),//2. Write SQL statement $sql =" Update Nation set Name= ' orc ' where code= ' n013 ' ";//3. Executes the SQL statement//$r = $pdo->query ($sql), returns another object after executing the query, and needs to find "= $pdo->exec ($sql) through another object;*///The transaction function is either fully executed or all failed//created$dsn= "Mysql:dbname=1016;host=localhost";$pdo=NewPDO ($dsn, "root", "Database Password");//set the exception mode, throw an exception if an error occurs$pdo->setattribute (pdo::attr_errmode,pdo::errmode_exception);/*Set Property (name, value) Note Do not enclose double quotation marks*///Write SQL statements$sql 1= "INSERT into nation values (' n007 ', ' Korean ')";$sql 2= "INSERT into nation values (' n008 ', ' Yi ')";//start a transaction, execute two SQL statements, eventually commit to the transaction, and then the transaction will determine if there is a problem and throw the problem if there is a problemTry//try to do it.{    //Start a transaction    $pdo-BeginTransaction (); $pdo-exec($sql 1); $pdo-exec($sql 2); //Commit a transaction    $pdo-commit ();}Catch/*caught*/(pdoexception$e)//a type of PDO throw{    //$e->getmessage (); Show error message// rollback    $pdo-rollBack ();}/*final//Final execution of the content, out of error are executed, the general write off code (PHP is not common) {}*/?></body>1107test.php

<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >PHP//created Objects$dsn= "Mysql:dbname=1016;host=localhost";$pdo=NewPDO ($dsn, "root", "Database Password");//Write SQL statements, preprocessing statements$sql= "SELECT * From Nation";//ready to execute$st=$pdo->prepare ($sql);//Execution$st-execute ();//read data fetch one line or fetch allVar_dump($st->fetchall (PDO::Fetch_assoc));?></body>1107chaxun.php

<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >PHP//created Objects$dsn= "Mysql:dbname=1016;host=localhost";$pdo=NewPDO ($dsn, "root", "Database Password");//Write SQL statements, pre-processing statements, using the name placeholder (name placeholder) preceded by a colon$sql= "INSERT into nation values (: Code,:name)";//ready to execute$st=$pdo->prepare ($sql);//Binding Parameters/*$st->bindparam (": Code", $code, PDO::P aram_str); $st->bindparam (": Name", $name, PDO::P aram_str); $code = " n011 "; $name =" Tujia nationality ";*///using associative arrays$attr=Array("Code" = "n012", "name" = "Buyi");//Execution$st->execute ($attr);//Summary: Name placeholder, with an associative array;? placeholder, indexed array of?></body>1107namezhanwei.php

<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >PHP//created Objects$dsn= "Mysql:dbname=1016;host=localhost";$pdo=NewPDO ($dsn, "root", "Database Password");//Write SQL statement, preprocessing statement with question mark occupy position$sql= "INSERT into nation values (?,?)";//prepares the SQL statement to return the statement object with a variable to receive the returned statement object$st=$pdo->prepare ($sql);//bind parameter bind binding param parameter starting from 1 index with question mark stance/*Simplified Pre-$st->bindparam (1, $code), $st->bindparam (2, $name);//Can be assigned in the back or in front $code= "n022"; $name = "Dwarf clan" ;*///simplified array of arrays$attr=Array("n010", "Mongolian");//commit execution, not to SQL statements, has passed.Var_dump($st->execute ($attr));//Execute successfully returns TRUE if the failed return false//preprocessing statement is used in the. placeholder, give the array the time to give the index array, order to correspond to?></body>1107sqlzhuru.php

PDO (20161107)

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.