pdo--php Data Objects

Source: Internet
Author: User

PDO environment configuration: Enable PDO to 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]]); Where: DSN database connection information such as "Mysql:host=localhost;dbname= Library name" DSN format: "Driver name: host= hostname; dbname= Library name" Username: User name 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 execute query SQL statements. Returns the Pdostatement object 2.  EXEC ($sql); Used to perform the increment, delete, change operation, return the influence row number; 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 get 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 single quotation marks 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 to handle the parameterized SQL and perform 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.    Parameters: 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 schemas (SQL statements and Objects) 2, Fetchall () return all results in a single call, with the result that the parameters are saved 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 executing 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 6 that requires the result collection. 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]]]) Where: $parameter: Placeholder name or index offset & $variable: The value of the parameter, which needs to be passed by reference that must be put in a variable where arguments: $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 the corresponding value is automatically assigned to the variable each time the row record is fetched. 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 executes again in PDO with two types of SQL statements (preprocessed SQL): 1.insert into Stu (id,name) value (?,?); /? 2.insert into Stu (id,name) value (: id,:name),//alias (for many parameters) in PDO, there are three types of parameters assigned to a parameterized SQL statement: 1. Using an array $stmt->execute (AR  Ray ("lamp1404", "qq2"));   $stmt->execute (Array ("id" = "lamp1404", "name" = "QQ2")); 2. Use the method to assign a single value $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. Use the method 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.  (If you insert multiple data at one time, one execution 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 transactional) MySQL common table types: MyISAM (non-transactional) fast, InnodB (transactional) High security//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, you can manipulate the database using the method in 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.