PDO--PHP Data Objects

Source: Internet
Author: User
Tags dsn format

Environment configuration of PDO: enable support for PDO in php. enable extension = php_pdo.dllextension = php_pdo_mysql.dll in the ini configuration file. classes involved in the PDO operation: PDO, PDOStatement (preprocessing object), PDOException (exception class) I. Construction Method of the PDO class: revoke PDO _ construct (string dsn [, string username [, string password [, array driver_options]); where: dsn database connection information, such as "mysql: host = localhost; dbname = database name "dsn format:" driver name: host = host Name; dbname = database name "username: User Name password: Password driver_options: configuration options: for example: PDO: ATTR_PERSISTENT => true, whether to enable persistent link * PDO: ATTR_ERRMODE => error handling mode: (three options can be used) (3) PDO: ERRMODE_SILENT: no error (ignore) (0) PDO: ERRMODE_WARNING: Warning (1) * PDO: ERRMODE_EXCEPTION: an error is reported in an abnormal way (recommended ). (2) $ pdo = new PDO ("mysql: host = localhost; dbname = lamp36db", "root", "root"); $ pdo-> setAttribute (PDO :: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION); Other Methods: -------------------------------------------------------- 1. query ($ SQL) is used to execute the query SQL statement. Return PDOStatement object 2. exec ($ SQL); used for adding, deleting, and modifying operations, Return Affected rows; 3. getAttribute (); get a "database connection object" attribute. 4. setAttribute (); set a "database connection object" attribute. 5. beginTransaction start a transaction (do a rollBack point) 6. commit transaction 7. rollBack transaction rollBack. 8. errorCode get error code 9. errorInfo get error message 10. lastInsertId get the added primary key value. 11. prepare creates the SQL preprocessing and returns the PDOStatement object 12. quote to add single quotation marks for the SQL string. Preprocessing object PDOStatement object: ========================================================== ===== we can use the PDO method to obtain PDOStatement: 1. obtain the query (query SQL) method of PDO for parsing the result set 2. obtain the prepare (SQL) method of PDO, which is used to process parameter SQL statements and perform operations. PDOstatement object method: ---------------------------------------------------------------- 1. fetch () returns the next row of the result set, move the result pointer down, and return false to the header. Parameters: PDO: FETCH_BOTH (default),: Index and associated array mode PDO: FETCH_ASSOC,: Joined array mode PDO: FETCH_NUM,: Index Array mode PDO: FETCH_OBJ ,: object Mode: PDO: FETCH_LAZY: all modes (SQL statements and objects) 2. fetchAll () returns all results through one call. The result is saved as an array parameter: PDO :: FETCH_BOTH (default), PDO: FETCH_ASSOC, PDO: FETCH_NUM, PDO: FETCH_OBJ, PDO: FETCH_COLUMN indicates taking a specified column, for example: $ rslist = $ stmt-> fetchAll (PDO: FETCH_COLUMN, 2); take the third column 3, execute () is responsible for executing a prepared preprocessing Statement 4. fetchColumn () returns the value of a column in the next row of the result set 5. setF EtchMode () is used to set the type of the result set. 6. rowCount () returns the total number of affected rows after the operation statement is added, deleted, modified, and queried. 7. setAttribute () is used to set attribute 8 for a preprocessing statement. getAttribute () Get a declared attribute 9. errorCode () Get error code 10. errorInfo () Get error message 11. bindParam () binds the parameter to the corresponding query placeholder bool PDOStatement: bindParam (mixed $ parameter, mixed & $ variable [, int $ data_type [, int $ length [, mixed $ driver_options]) Where: $ parameter: placeholder name or index offset & $ variable: parameter value, which must be passed by reference, that is, a variable must be placed with parameters: $ data_type: Data Type PD O: PARAM_BOOL/PDO: PARAM_NULL/PDO: PARAM_INT/PDO: PARAM_STR/PDO: PARAM_LOB/PDO: PARAM_STMT/PDO: PARAM_INPUT_OUTPUT $ length: index data type length $ driver_options: Driver option. 12. bindColumn () is used to match the column name and a specified variable name. In this way, the corresponding value is automatically assigned to the variable each time a row record is obtained. 13. bindValue () binds a value to a corresponding parameter 14. nextRowset () checks the next row of set 15. the number of columnCount () returned columns in the result set 16. getColumnMeta () returns the attribute information of a column in the result set 17. closeCursor () closes the cursor so that the statement can be re-executed in the PDO parameter-type SQL statement (preprocessing SQL): 1. insert into stu (id, name) value (?,?); //? Expression 2. insert into stu (id, name) value (: id,: name); // The Alias type (suitable for multiple parameters). In PDO, there are three types of values assigned to the parameter SQL statement: 1. use an array $ stmt-> execute (array ("lamp1404", "qq2"); $ stmt-> execute (array ("id" => "lamp1404 ", "name" => "qq2"); 2. assign $ stmt-> bindValue (1, "lamp1901"); $ stmt-> bindValue (2, "qq2"); $ stmt-> execute (); $ stmt-> bindValue (": id", "lamp1901", PDO: PARAM_STR); // $ stmt-> bindValue (": name ", "qq2", PDO: PARAM_STR); $ stmt-> execut E (); 3. bindParam (": id", $ id); $ stmt-> bindParam (": name", $ name ); $ id = "lamp1401"; $ name = "qq2"; $ stmt-> execute (); transaction processing ----------------------------------------- transaction: add, delete, and modify Multiple SQL operations) as an operation unit, either success or failure. (If multiple data entries are inserted at a time, one operation fails, data is rolled back, and all data is deleted) ----- 4. PDO supports transactions. 1. The operated table must be an innoDB table (supports transactions). MySQL commonly used table Type: MyISAM (non-Transaction) fast addition, deletion, and modification, and high InnodB (transaction) security // change the table type to innoDB mysql> alter table stu engine = innodb; Query OK, 29 rows affected (0.34 sec) records: 29 Duplicates: 0 Warnings: 0 // view the table structure mysql> show create table stu \ G; 2: Use PDO to operate the database and use the PDO method: beginTransaction enables a transaction (for a rollBack point) commit to submit the transaction rollBack operation. Usage: when processing multiple SQL statements (adding, deleting, modifying, and modifying), the operation must be successful.


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.