The basic operation of PDO for MySQL

Source: Internet
Author: User
Tags dsn getmessage rowcount

PDO expansion Operations<?PHP$dsn= ' Mysql:dbname=yii2;host=localhost ';$user= ' Root ';$password= ' 123456 ';Try{    $DBH=NewPDO ($dsn,$user,$password,Array(Pdo::attr_errmode = PDO::errmode_exception));}Catch(pdoexception$e){    Echo' Connection failed: '.$e-getMessage ();}//Transaction Usage-begintransaction (), Commit (), RollBack (), exec ()/*//Added the last parameter in New PDO () try{$dbh->begintransaction ();    $sqlDel = "Delete from country where code = ' PK '";    $sqlIn = "INSERT INTO country (CODE,NAME,POP) VALUES (' TT ', ' TEST ', 9999)";    $DBH->exec ($sqlDel);    $DBH->exec ($sqlIn); $DBH->commit ();}    catch (Pdoexception $e) {echo "<br/>error:<br/>";    echo "<pre>";    Print_r ($e->getmessage ()); $DBH->rollback ();}*///Transaction Use-SetAttribute (), BeginTransaction (), Commit (), RollBack ()/*//Set the error mode, be sure to set it, otherwise it will not roll back and throw an exception, or you can add this value to the last parameter of the new PDO ($dbh->setattribute) (Pdo::attr_errmode,pdo::errmode_     EXCEPTION); try{$dbh->begintransaction ();     $sqlDel = "Delete from country where code = ' GX '";     $sqlIn = "INSERT INTO country (code,name,population) VALUES (' PK ', ' good ', ' 4444444 ')";     $delFlag = $dbh->exec ($sqlDel);     $inFlag = $dbh->exec ($sqlIn);     Var_dump ($delFlag);     Var_dump ($inFlag);    Echo ' Commit '; Var_dump ($DBH->intransaction ());         True $DBH->commit ();    Var_dump ($DBH->lastinsertid ()); Echo ' commit222222 ';}    catch (Pdoexception $e) {echo ' rollBack ';    $DBH->rollback (); echo $e->getmessage ();} $DBH->setattribute (pdo::attr_autocommit,1);*///Delete-exec ()/*$sql = "Delete from country where code = ' FK '"; $count = $dbh->exec ($sql); Var_dump ($count);//int (1) int (0)*///New-exec ()/*$sql = "INSERT INTO country (code,name,population) VALUES (' FK ', ' yes ', 13000)", $count = $dbh->exec ($sql); var_dump ($count); Int (1)*///queries-query ()/*$sql = "SELECT * from country where code = ' AU '"; $res = $dbh->query ($sql, Pdo::fetch_assoc); foreach ($res as $row) {    echo "<pre>"; Print_r ($row);} Array ([Code] = AU [name] = Australia [Population] = 18886000)*///Query-fetchall ()/*$sql = "SELECT * from country where code =: Code"; $sth = $dbh->prepare ($sql); $sth->execute (Array (": Code" = "A U ")); $res = $sth->fetchall (PDO::FETCH_ASSOC); can also be used in $DBH->setattribute (PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC), set only associative array print_r ($res);*//*Array ([0] = = Array ([code] = au [0] = = au [name] = = Australi A [1] = Australia [Population] = 18886000 [2] = 18886000)) Array ([ 0] = = Array ([code] + AU [name] + Australia [population] = 1888600 0))*///pdostatement Operation<?php//http://php.net/manual/zh/pdostatement.execute.php$dsn= ' Mysql:host=localhost;dbname=yii2 ';$username= ' Root ';$password= ' 123456 ';Try{    $DBH=NewPDO ($dsn,$username,$password);}Catch(pdoexception$e){    Echo"Failure:"; Echo $e-GetMessage (); Exit();}Echo"<pre>";/*print a SQL preprocessing command-debugdumpparams$name = ' GD '; $sql = "SELECT * from country WHERE name =: Name"; $res = $dbh->prepare ( $sql) $res->bindvalue (": Name", $name), $res->execute (); $res->debugdumpparams (); $rr = $res->fetchall ( PDO::FETCH_ASSOC);p Rint_r ($RR); SQL: [+] SELECT * FROM country where name =: Nameparams:1key:name: [5]: nameparamno=-1name=[5] ": Name" IS_PARAM=1PARAM_ type=2*//*gets the number of columns of the record ColumnCount () $sql = "SELECT * from country"; $res = $dbh->prepare ($sql); $res->execute (); $RR = $res ColumnCount ();p rint_r ($RR); 3*//*returns the number of rows affected-rowCount (), prepare (), Bindvalue (), execute (), $code = ' PK '; $sql = "Update country set name = ' GD ' where code =: Code "; $res = $dbh->prepare ($sql); $res->bindvalue (": Code ", $code); $res->execute (); $affectCount = $res- >rowcount ();p rint_r ($affectCount); 1*//*Query-Prepare (), Bindvalue (), Fetchall (), execute () $name = ' good '; $sql = "SELECT count (1) as total from country where Nam E =: Name "; $res = $dbh->prepare ($sql); $res->bindvalue (": Name ", $name); $res->execute (); $RR = $res Fetchall (PDO::FETCH_ASSOC);p rint_r ($RR); Array ([0] = = Array ([total] = 2))*//*Query-Bindvalue (), execute (), fetchall () $name = ' good '; $code = ' FK '; $sql = "SELECT * from country where name =? and code =? Limit 1 "; $res = $dbh->prepare ($sql); $res->bindvalue (1, $name); $res->bindvalue (2, $code); $res->execute () ; $rr = $res->fetchall (PDO::FETCH_ASSOC);p rint_r ($RR); Array ([0] = = Array ([code] = FK [name] + good [population] = 4 444444))*//*Query-Prepare (), Bindvalue (), execute (), fetchall () $name = "good"; $code = ' FK '; $sql = "SELECT * from country where name = : Name and code =: Code Limit 1 "; $res = $dbh->prepare ($sql); $res->bindvalue (": Code ", $code); $res->bindvalue (": Name ", $name), $res->execute (), $RR = $res->fetchall ();p rint_r ($RR);            Array ([0] = = Array ([code] = FK [0] = = FK [name] = = Good [1] = = good [Population] = 4444444 [2] = 4444444))*//*Query-Prepare (), Bindparam (), execute (), fetchall () $name = ' good '; $code = ' PK '; $sql = "SELECT * from country where name = ? and code =? "; $res = $dbh->prepare ($sql); $res->bindparam (1, $name); $res->bindparam (2, $code); $res->execute (); $RR = $ Res->fetchall (PDO::FETCH_ASSOC);p rint_r ($RR); Array ([0] = = Array ([code] = PK [name] + good [population] + 4 444444))*///Query-Prepare (), Bindparam (), execute (), Fetchall ()/*$name = ' good '; $code = ' PK '; $population = 4444444; $sql = "SELECT * from country WHERE name =: Name and code =: Code an D population =:p opulation "; $res = $dbh->prepare ($sql); $res->bindparam (": Code ", $code); $res->bindparam (": Name ", $name, PDO::P aram_str), $res->bindparam (":p opulation ", $population); $res->execute (); $RR = $res Fetchall (PDO::FETCH_ASSOC);p rint_r ($RR); Array ([0] = = Array ([code] = PK [name] + good [population] + 4 444444))*///Query-Prepare (), execute (), Fetch ()/*$sql = "SELECT * FROM Country limit 2", $res = $dbh->prepare ($sql), $res->execute (), while ($rs = $res->fetch (PD O::FETCH_ASSOC)) {Print_r ($rs);} Array ([code] = AU [name] = + Australia [population] = 18886000) array ([code] = BR [name] =&G T Brazil [Population] = 170115000)*///Query-Prepare (), execute (), Fetchall ()/*$sql = "SELECT * FROM country limit 1"; $res = $dbh->prepare ($sql); $res->execute (); $rr = $res->fetchall ();p RI Nt_r ($RR);            Array ([0] = = Array ([code] = au [0] = = au [name] = = Australia [1] = = Australia [Population] = 18886000 [2] = 18886000))*///Query-Prepare (), execute (), Fetchall ()/** $sql = "SELECT * FROM country limit 1"; $sth = $dbh->prepare ($sql); $sth->execute (); $res = $sth->fetchall (PDO :: FETCH_ASSOC); echo "<pre>";p Rint_r ($res); Array ([0] = = Array ([code] = AU [name] = + Australia [population] =& Gt 18886000))*/

The basic operation of PDO for MySQL

Related Article

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.