Connection and use of the php pdo Database

Source: Internet
Author: User
<? PHP/** operations on the PDO database **/$ DNS = "MYSQL: dbname = BBS; host = localhost"; $ user = "root"; $ Password = ""; $ opt = array (PDO: attr_persistent => true); $ PDO = new PDO ($ DNS, $ user, $ password, $ OPT);/** getattribute () view Database Connection Properties **/var_dump ("Disable Automatic submission function :". $ PdO-> getattribute (PDO: attr_autocommit); var_dump ("the mode in which PDO errors are handled :". $ PdO-> getattribute (PDO: attr_errmode); var_dump ("case-sensitive conversion of table field characters :". $ PdO-> getattribute (PDO: attr_case); var_dump ("special information related to the connection status :". $ PdO-> getattribute (PDO: attr_connection_status); var_dump ("convert an empty string to a SQL NULL :". $ PdO-> getattribute (PDO: attr_oracle_nulls); var_dump ("Application Program Get data size in advance :". $ PdO-> getattribute (PDO: attr_persistent); var_dump ("server information unique to the database :". $ PdO-> getattribute (PDO: attr_server_info); var_dump ("Database Server version :". $ PdO-> getattribute (PDO: attr_server_version); var_dump ("database Client Version :". $ PdO-> getattribute (PDO: attr_client_version);/** two execution methods: * Exec: return the number of affected rows for insert Delete update * query, and return a pdostatement, operations on each piece of information ** // ** $ SQL = "delete from news where id = 16"; Echo $ PdO-> exec ($ SQL ); ------------------------------------------------ quote for SQL statements escape $ SQL = $ PdO-> quote ("delete from news where id = '1'"); echo $ SQL; // output: 'delete from news where id = \ '1 \ ''' -------------------------------------------------- lastinsertid $ SQL = "insert into news values ('', 'Boy ', 'Boy ')"; $ PdO-> exec ($ SQL); echo "ID of the inserted record :". $ PdO-> lastinsertid (); // obtain the ID of the last inserted record ------------------------ -------------------------- One of the most important methods is prepare ($ SQL), which returns a pdostatement. Similarly, another method for returning pdostatement is query () used to query data prepare ($ SQL) is to pre-process SQL statements and return a pdostatement object, it contains a lot of useful methods. Next section record the pdostatement attribute and legal -------------------------------------------------- The following is the transaction operation ***/$ sql1 = "delete from news where id = 21 "; $ sql2 = "delete from news where id = 22"; $ PdO-> setattribute (PDO: attr_errmode, PDO: errmode_mode t Ion); try {$ PdO-> begintransaction (); $ PdO-> exec ($ sql1); $ PdO-> exec ($ sql2);} catch (exception $ ex) {$ PdO-> rollback (); echo "database operation failed ". $ ex-> getmessage () ;}?>

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.