"PHP PDO" pure PHP (without frame) how to use Mysql PDO

Source: Internet
Author: User

1 Configuration information

$config = Array (' db ' = = Array (' host ' = ' = ' 127.0.0.1 ', ' user ' = ' root ', ' pass ' = ' = ' ', ' db ' = ' = ' test_db ', ' DNS ')       = ' Mysql:dbname=test_db;host=127.0.0.1;charset=utf8 '))

Configure database links, user names, passwords, libraries, DNS information (including database name, database link IP, and character set), respectively

Note: If you do not set character sets, the Chinese data stored in the database may still be garbled, even if the database is already set to UTF8 (the character set that needs to keep the code, database settings, linked database remains UTF8)


2 Linked databases

try {    $db = new PDO ($config [' db '] [' DNS '], $config [' db '] [' user '], $config [' db '] [' Pass ']), and catch (Pdoexception $e) { C3/>echo ' Connection failed: '. $e->getmessage (); exit;}

3.1 Queries

Query  $sql 1 = "SELECT * from Tbl_test1 WHERE condition1 =: condition1 and Condition2 =: Condition2";  $sql _data1 = Array (  ": Condition1" + 1,  ": condition2" = "abc"  );  $sth 1 = $db->prepare ($sql 1);  $sth 1->execute ($sql _data1); Get a  $result 1 = $sth 1->fetch (PDO::FETCH_ASSOC);  Get all  //$result 1 = $sth 1->fetchall (PDO::FETCH_ASSOC);    Determine if successful  if ($result 1) {  //query succeeded  }else{  //query failed  }

3.2 Update

Update  $sql 2 = "Update tbl_test1 SET ' key1 ' =: val1, ' key2 ' =: val2 WHERE  condition1 =: condition1 and condition2 = : Condition2 ";  $sql _data2 = Array (  ": Val1" + 1,  ": val2" = "Hello",  ": Condition1" and "= 1,  ": condition2 "=" ABC "  );  $sth 2 = $db->prepare ($sql 2);  $sth 2->execute ($sql _data2);    Determine if successful  if ($sth 2->rowcount () >0) {  //update succeeded  }else{  //update failed  }  

3.3 Inserting

Insert  $sql 3 = "INSERT INTO Tbl_test1 (' Key1 ', ' key2 ', ' Key3 ', ' key4 ', ' Key5 ') VALUES (: Val1,: Val2,: Val3,: VAL4,: VA L5) ";  $sql _data3 = Array (  "Val1" = 1,  "val2" = "Hello",  "val3" = 100.25,  "Val4" and "Write  " VAL5 "=" 2015-10-30 "  );  $sth 3 = $db->prepare ($sql 3);  $result 3 = $sth 3->execute ($sql _data3);    Determine if successful  if ($result 3) {  //Insert succeeded//  the newly inserted data of the self-growth ID  //$db->lastinsertid ();  } else{  //insert failed  }  


"PHP PDO" pure PHP (without frame) how to use Mysql PDO

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.