Pdo database operations

Source: Internet
Author: User

The pdo technology will be fully used in php6. It seems that this technology must have its extraordinary advantages. After reading it, I learned it and recorded the notes, but it was not so vulnerable ~ \ (Too many rows )/~ La la

<? Php // You must configure the extension and php before using pdo. ini try {// $ pdo = new PDO ("mysql: host = localhost; dbname = pd", "root", "", array (PDO :: ATTR_AUTOCOMMIT => 0); // tem connection to the database method dsn, and handling error display method. When you change the database, you only need to change the connection. array () you can also use the following statement to set the error report mode // $ pdo-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_WARNING ); // you can use $ pdo-> errorInfo () to output error messages, but the capture exception does not apply to $ pdo-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION );} catch (PDOException $ e) {echo 'Connection failed'. $ E-> getMessage (); // get exception information exit ();} // SQL exec --> update, delete, insert, other; query --> with result set, select; prepare // echo $ pdo-> exec ("insert into test (title, content) values ('wangw', 'Thank you! ') "); // Returns the number of affected rows 1 // sets the error report mode // echo $ pdo-> errorCode (). '<br>'; // print_r ($ pdo-> errorInfo (); // A // set the constant // echo $ pdo-> lastInsertId (); // $ arr = $ pdo-> query ("select * from test"); // tentatively processed result set/* transaction processing: Any of the following links has an error, all the statements are executed. John bought a 2000 yuan computer from James and deducted 2000 from Michael. Add 2000 to James. Reduce the difference between MyIsAM InnoDB and MyIsAM InnoDB: the latter type provides the transaction processing function. If you do not need to process the transaction, use MyIsAM. * // try {// $ pdo-> beginTransaction (); // $ price = 50; // $ affect_rows = $ pdo-> exec ("updat E zhanghao set price = price-{$ price} where id = '1' ") or die ($ pdo-> errorInfo (); // if (! $ Affect_rows) // throw new PDOException ("Michael Jacob transfer failed "); // $ affect_rows = $ pdo-> exec ("update zhanghao set price = price + {$ price} where id = '2'"); // if (! $ Affect_rows) // throw new PDOException ("failed to transfer to Li Si"); // echo "transaction successful"; // $ pdo-> commit (); //} catch (PDOException $ e) {// echo $ e-> getMessage (); // $ pdo-> rollBack (); ///} // $ pdo-> setAttribute (PDO: ATTR_AUTOCOMMIT, 1); /// compare the result set of preprocessing and/* preparment to exec, query efficiency is higher, even if it is a single statement; security; * // * are two placeholder symbols in pdo recommended? Parameter ---- index array, use name parameter in the order of index ----- join the array, use by name, and order independent */$ pdo = new PDO ("mysql: host = localhost; dbname = pd "," root "," "); // $ stmt = $ pdo-> prepare (" insert into test (title, content) values (?,?) "); // All SQL statements can execute a returned object. // a prepared statement is incorporated into the server and compiled, just assign the data to him. // $ stmt = $ pdo-> prepare ("insert into test (title, content) values (: title,: content )"); // update is the same as others. // bind the parameter // $ stmt-> bindParam (": content", $ content ); // pass the reference type value // $ stmt-> bindParam (": title", $ title); // $ title = 'God '; // $ content = 'I am fine! ';//////? Form // $ stmt-> bindParam (1, $ title); // reference type to pass the value /// $ stmt-> bindParam (2, $ content ); //// $ title = 'ljf'; // $ content = 'Welcome! '; // If ($ stmt-> execute () // {echo "executed successfully"; echo $ pdo-> lastInsertId ();} // else // echo "execution failed"; // $ stmt-> execute (array (": title" => "oh ",": content "=>" you are sexy "); // if the form is $ stmt-> execute ($ _ POST ); // $ stmt-> execute (array ("look", "it's a beauty ")); // obtain the result $ stmt = $ pdo-> prepare ("select id, title, content from test where id>: id order by id desc "); $ stmt-> execute (array (": id" => 2); // stmp is the object of the result set // $ stm T-> setFetchMode (PDO: FETCH_ASSOC); // you can specify the returned array format. // while ($ row = $ stmt-> fetch (PDO: FETCH_ASSOC )) // returns a query result in the form of an array returned: PDO: FETCH_NUM index, and FETCH_ASSOC associated array .... both default --- no need to write it after setting it. // print_r ($ row ). '<br>'; $ data = $ stmt-> fetchAll (PDO: FETCH_ASSOC); echo '<pre>'; print_r ($ data ); echo '</pre>'; // The total number of records, column information, many .... echo $ stmt-> rowCount (); // total number of records?>

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.