PDO Data Access Abstraction Layer

Source: Internet
Author: User

//pdo//data Access Abstraction Layer<?PHP//1. Operation of other databases//2. transaction function//3. Prevent SQL Injection Attack//Build PDO object//$dsn = "mysql:dbname=mydb;host=localhost";//Data source//$pdo = new PDO ($DSN, " Root "," 123 ");//write SQL statement//$sql =" SELECT * from Nation ";//$sql =" INSERT into nation values (' n077 ', ' data ') ";//Execute, The Pdostatement object is returned//$a = $pdo->query ($sql); Execute Query//$a = $pdo->exec ($sql); Execute other statements//var_dump ($a);//$arr = $attr->fetchall (pdo::fetch_both);//var_dump ($arr);//Transaction function/transaction: Ability to control statements while successfully failing simultaneously Can roll back when failed$dsn= "Mysql:dbname=mydb;host=localhost"; Database name, server$pdo=NewPDO ($dsn, "root", "123");//Set Exception mode$pdo->setattribute (pdo::attr_errmode,pdo::errmode_exception); Try{    //Open Transaction    $pdo-BeginTransaction (); $sql 1= "INSERT into nation values (' n080 ', ' delete ')"; $sql 2= "INSERT into nation values (' n070 ', ' several ')"; $sql 3= "INSERT into nation values (' n075 ', ' several ')"; $pdo-exec($sql 1); $pdo-exec($sql 2); $pdo-exec($sql 3);       Three execution success is the same as the relationship, otherwise an error occurs. //Submit    $pdo-commit ();}Catch(Exception $e){    //grab the error that appears in the try and handle the Zv//echo $e->getmessage ();//Get exception information //Rollback    $pdo-rollBack ();}//Final ()//{//final execution, no matter if the above try code is not error, will execute/}?></body>
<?PHP//preprocessing statements to prevent SQL injection$dsn= "Mysql:dbname=mydb;host=localhost";$pdo=NewPDO ($dsn, "root", "123");//$code = "n005"; Do you want to add a placeholder in the//sql statement?//$sql = "SELECT * from Nation where code=?";$sql= "INSERT into nation values (?,?)";//ready to execute, return Pdostatement object$st=$pdo->prepare ($sql);//call the Bind parameter's method to bind the parameter//$st->bindparam (1, $code);//$st->bindparam (2, $name);//$name = "Test 1";//Index array/ /Note the difference$attr=Array("n006", "Test 2");//Execution Method$st->execute ($attr);//$attr = $st->fetchall ();//var_dump ($attr);?>
<?PHP$dsn= "Mysql:dbname=mydb;host=localhost";$pdo=NewPDO ($dsn, "root", "123");//placeholder is a string$sql= "INSERT into nation values (: Code,:name)";$st=$pdo->prepare ($sql);//wait for execution. Prepare preparation//$st->bindparam (": Code", $code, PDO::P aram_str);//$st->bindparam (": Name", $name, PDO::P aram_str); /$code = "n007";//$name = "Test 3";//associative array//with? The difference between placeholders$attr=Array("Code" = "n008", "name" = "Test 4");$st->execute ($attr);?>

PDO Data Access Abstraction Layer

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.