[PHPPDO] note on how to use MysqlPDO in pure PHP (without framework)

Source: Internet
Author: User
1 configuration information $ configarray (dbarray (host127.0.0.1, userroot, pass, dbtest_db, dnsmysql: dbnametest_db; host127.0.0.1; charsetutf8) Configure database links, user names, passwords, libraries, dns information (including the database name, database link ip address, and Character Set) Note: If

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 the database link, user name, password, database, dns information (including the database name, database link ip address, and Character Set) respectively. Note: If

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 the database link, user name, password, database, and dns information respectively (including the database name, database link ip address, and Character Set)

Note: If the character set is not set, even if utf8 is already set in the database, the Chinese data stored in the database may still be garbled (the code, database settings, and character set for linking to the database must be UTF-8)


2. Connect to the database

try {    $db = new PDO($config['db']['dns'], $config['db']['user'], $config['db']['pass']);} catch (PDOException $e) {    echo 'Connection failed: ' . $e->getMessage();exit;}

3.1 Query

// Query $ sql1 = "SELECT * FROM tbl_test1 WHERE condition1 =: condition1 and condition2 =: condition2"; $ SQL _data1 = Array (": condition1" => 1 ,": condition2 "=>" abc "); $ sth1 = $ db-> prepare ($ sql1); $ sth1-> execute ($ SQL _data1 ); // get a $ result1 = $ sth1-> fetch (PDO: FETCH_ASSOC); // get all // $ result1 = $ sth1-> fetchAll (PDO: FETCH_ASSOC ); // determine whether the query is successful if ($ result1) {// query successful} else {// query Failed}

3.2 update

// UPDATE $ sql2 = "UPDATE tbl_test1 SET 'key1' =: val1, 'key2' =: val2 WHERE condition1 =: condition1 and condition2 =: condition2 "; $ SQL _data2 = Array (": val1" => 1, ": val2" => "hello", ": condition1" => 1 ,": condition2 "=>" abc "); $ sth2 = $ db-> prepare ($ sql2); $ sth2-> execute ($ SQL _data2 ); // determine whether the update is successful if ($ sth2-> rowCount ()> 0) {// update successful} else {// update failed}

3.3 insert

// INSERT $ sql3 = "insert into tbl_test1 ('key1', 'key2', 'key3', 'key4', 'key5') VALUES (: val1,: val2 ,: val3,: val4,: val5) "; $ SQL _data3 = Array (" val1 "=> 1," val2 "=>" hello "," val3 "=> 100.25, "val4" => "casually write" "val5" => "2015-10-30"); $ sth3 = $ db-> prepare ($ sql3 ); $ result3 = $ sth3-> execute ($ SQL _data3); // if ($ result3) {// successful insertion // The auto-increment id of the latest inserted data // $ db-> lastInsertId ();} else {// insertion Failed}

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.