pdo--php Data Objects

Source: Internet
Author: User

PDO environment configuration: Turn on support PDO open in php.ini config file: Extension=php_pdo.dllextension=php_pdo_mysql.dll involves classes in PDO operations: PDO, Pdostatement ( preprocessing objects), Pdoexception (Exception Class), PDO class construction method:---------------------------------------------------------PDO __construct ( String DSN [, String username [, string password [, array driver_options]]); In: DSN database connection information such as "Mysql:host=localhost;dbname= Library name" DSN format: "Driver name: host= hostname; dbname= Library name" Username: username Password: Password dr Iver_options: Configuration options: such as: pdo::attr_persistent=>true, whether to turn on persistent link *pdo::attr_errmode=> error handling mode: (can be the following three) (3) PDO:: Errmode_silent: Do not Report errors (ignore) (0) pdo::errmode_warning: Warning Error (1) *pdo::errmode_exception: Error (recommended) in an abnormal manner. (2) $pdo = new PDO ("mysql:host=localhost;dbname=lamp36db", "root", "root"), $pdo->setattribute (Pdo::attr_errmode, Pdo::errmode_exception); Other methods:--------------------------------------------------------1. Query ($sql); Used to run query SQL statements. Returns the Pdostatement object 2.  EXEC ($sql); Used for operation increment, delete, change operation, return affect the number of rows; 3. GetAttribute (); Gets a database Connection object property. 4. SetAttribute (); Set a database Connection object property. 5. BegintransactiOn open a thing (make a rollback point) 6. Commit COMMIT TRANSACTION 7. Rollback the transaction rollback operation. 8. ErrorCode get error code 9. ErrorInfo gets the error message 10.lastInsertId gets the primary key value that was just added. 11.prepare creates a preprocessing of SQL, returns a Pdostatement object 12.quote adds a single argument to the SQL string. Preprocessing Object Pdostatement object: ============================================= We can get pdostatement:1 by means of PDO. PDO query (Query SQL) method gets the prepare (SQL) method used to parse the result set 2.PDO, which is used to process the parameter SQL and run the operation. Method of Pdostatement object:----------------------------------------------------------------1, Fetch () returns the next row of the result set, the result pointer moves down,  The head returns FALSE.    References: Pdo::fetch_both (default),: Index plus associative array pattern PDO::FETCH_ASSOC,: Associative array pattern pdo::fetch_num,: Indexed array pattern pdo::fetch_obj, : Object mode Pdo::fetch_lazy: All modes (SQL statements and Objects) 2, Fetchall () returns all results in a single call, with the result of saving the parameters as an array: Pdo::fetch_both (default), Pdo::fetch _assoc, Pdo::fetch_num, pdo::fetch_obj, pdo::fetch_column indicate that a column is specified, such as: $rslist = $stmt->fetchall (pdo::fetch_column, 2); Take the third column 3, execute () is responsible for running a prepared preprocessing statement 4. Fetchcolumn () returns the value of a column for the next row in the result set 5. Setfetchmode () Sets the type of the required result collection to 6. RowCount () returns the total number of rows affected after using the Add, delete, change, and check operation statements 7. SetAttribute () Sets property 8 for a preprocessing statement. GetAttribute () Gets a declarationThe property of 9. ErrorCode () gets error code 10. ErrorInfo () gets error message 11.  Bindparam () binds the parameter to the corresponding query placeholder on bool Pdostatement::bindparam (mixed $parameter, mixed & $variable [, int $data _type [, int $length [, mixed $driver _options]]): $parameter: Placeholder name or index offset & $variable: The value of the argument, which needs to be passed by reference, that is, a variable must be put in the number of parameters: $data _ Type: Data type PDO::P aram_bool/pdo::P aram_null/pdo::P aram_int/pdo::P aram_str/pdo::P aram_lob/pdo::P aram_stmt/pdo:: Param_input_output $length: The length of the exponential data type $driver _options: drive option. Bindcolumn () is used to match the column name and a specified variable name so that each time a row record is fetched, the corresponding value will be assigned to the variable on its own initiative. Bindvalue () binds a value to one of the corresponding parameters in 14. Nextrowset () checks the next line set 15. ColumnCount () returns the number of columns in the result set 16. Getcolumnmeta () Returns the property information for a column in the result set 17. Closecursor () Closes the cursor so that the declaration runs again in the PDO, there are two SQL statements (pre-SQL): 1.insert into Stu (id,name) value (?,?); /? 2.insert into Stu (id,name) value (: id,:name),//alias (for more than one) in PDO, there are three types of assignment SQL statements: 1. Using an array $stmt->execute (AR  Ray ("lamp1404", "qq2"));   $stmt->execute (Array ("id" = "lamp1404", "name" = "QQ2")); 2. Usage Single assignment $stmt->bindvalue (1, "lamp1901 "); $stmt->bindvalue (2, "qq2"); $stmt->execute (); $stmt->bindvalue (": id", "lamp1901", PDO::P aram_str); With the specified type $stmt->bindvalue (": Name", "Qq2", PDO::P aram_str);    $stmt->execute (); 3. Usage binding variable $stmt->bindparam (": id", $id); $stmt->bindparam (": Name", $name); $id = "lamp1401";     $name = "QQ2"; $stmt->execute (); Transactional-----------------------------------------------transactions: Multiple SQL Operations (redaction) as an action unit, either succeed or fail.  (Suppose you insert multiple data at one time, one run fails, the data is rolled back, all is deleted)-----4. PDO support for transactions first: The table being manipulated must be a table of type InnodB (support transactions) MySQL frequently used table types: MyISAM (non-transactional) fast, InnodB (transaction type) security High//Change table type InnodB type mysql> ALTER TABLE Stu Engine=innodb;      Query OK, rows affected (0.34 sec) records:29 duplicates:0 warnings:0//view table structure mysql> show create table stu\g; Second: Using PDO to manipulate the database is a way to use the PDO: BeginTransaction to open a thing (make a rollback point) Commit COMMIT TRANSACTION ROLLBACK TRANSACTION rollback operation. Usage: As multiple SQL statements are processed (additional deletions), the requirement must be successful.


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.