001-PDO Usage Detailed analysis

Source: Internet
Author: User
This article introduces a detailed analysis of PDO usage and is now shared with everyone. If you need a friend, please refer to it.

PDO 41, role: To solve the user needs to use a different database when the switch back and forth, PDO can automatically switch the database. 42. Using PDO a) php.ini file to open PDO Extension 43, using PDO to operate the database basic step a) connection authentication I. $PDO = new PDO ("Mysql:host=localhost;port=3306;dbname=            Project "," root "," root ");    Ii.dbname is entered in database B) organization SQL statement I. $sql = "Show Tables";    c) Send SQL statement, receive execution result i. $STMT = $pdo->query ($sql); d) obtain data from the result set I. $stmt->fetch (); 44, PDO common function A) Stdclass standard class empty Class B) pdo:i.exec ($sql); Executes an SQL statement that returns the number of rows affected (for Additions and deletions) ii. $stmt = query ($sql); Execute SQL statement, return Pdostatement object (for check) c) pdostatement (result set) I. $stmt->fetch (); return a A collection of associative arrays + indexed arrays 1. Parameter 1PDO::FETCH_ASSOC returns only the associative array 2.2pdo::fetch_num only returns the indexed array ii. $stmt->fetchall () gets        All contents of the result set (parameters same as fetch ()) Iii. returns an object 1.class persion{} 2. $stmt->fetchobject (persion)            Iv.bindcolumn and fetch bind a column to a variable 1. $stmt->bindcolumn (' S_name ', $name);    2. $stmt->bindcolumn (3, $number); Note: The index starts at 1, 3. $stmtFetch (); 4.echo $name, $number D) PDO pretreatment I.:s_name can these all be used? (Indexed values when binding data), and ii. using arrays to specify preprocessing variables 1. Step: a) $sql = "INSERT into pro_student values (null,:s_n ame,:s_num,:s_gender,:s_age,:c_id) ";//sql b) $stmt = $pdo->prepare ($sql);//Send preprocessing C) to the preprocessing binding   Set data i $arr = Array (ii.   ': s_name ' = ' Jaycee ', iii.   ': s_num ' = ' itcast0001 ', Iv.   ': s_gender ' = ' male ', v. ': s_age ' = ', vi '.                ': c_id ' = ' 2 ', vii.) d) $stmt->execute ($arr);//Perform preprocessing iii. by binding variable Form 1. Step a) $sql = "INSERT INTO Pro_student                VALUES (null,:s_name,:s_num,:s_gender,:s_age,:c_id) ";//sql b) $stmt = $pdo->prepare ($sql);//Send preprocessing                    c) Bind data to preprocessing I. $name = "Li Mo sorrow";                    II. $num = "itcast0002"; Iii.$gender = "female";                    Iv. $age = "30";                    V. $c _id = "3";                    vi.//binds the variable to preprocessing vii. $stmt->bindparam (': S_name ', $name);                    Viii. $stmt->bindparam (': S_num ', $num);                    IX. $stmt->bindparam (': S_gender ', $gender);                    X. $stmt->bindparam (': S_age ', $age);                Xi. $stmt->bindparam (': c_id ', $c _id);            d) $stmt->execute ();//perform pre-processing e) PDO transaction processing I. Note: The storage engine of the data table must be the InnoDB iii. transaction process 1. $pdo = new PDO (' Mysql:host=localhost;port=3306;dbname=project ', ' root ', ' root ');//connection authentication 2. $res = $pdo->b                Egintransaction ();//Open transaction 3. Transaction a) $sql = "Updata pro_student set s_age=28 where s_id=20";                b) $lines = $pdo->exec ($sql);//Returns the number of rows affected c) $sql = "SELECT * from Pro_student where s_id=20";                d) $stmt = $pdo->query ($sql); e) $stmt->fetch(PDO::FETCH_ASSOC);                4. Commit TRANSACTION A) if ($links) {b) $pdo->commit ();//update succeeded C)}else{ d) $pdo->rollback ();//update failed rollback data e)} f) PDO property setting I. Sets how the PDO handles the data in the process of ii. Pdo::getattribute ();//Get Attribute III. Pdo::setattribute ();//Set Attribute Iv. Example: 1. $pdo = new PD (' Mysql:host=localhost;port=3306;dbname=project ', ' root ' , ' root ');//connection Authentication 2. $peo->getattrbute (pdo::attr_autocommit);//gets autocommit attribute 3. $pdo->setattrbute (pdo::attr_ autocommit,0)//set off Autocommit v. Parameter memory: 1.pdo::attr_case Force column name to specified case a) pdo::case_lower Force lowercase B) PDO:: Case_upper Mandatory Capital VI ...    For more information please refer to the manual ... g) PDO exception handling i.try{ii.    Set error handling mode (must be set to take effect) III.    $pdo->setattribute (pdo::attr_errmode,pdo::errmode_exception); Once an error is entered, immediately enter the catch statement v.}    catch (Pdoexception $e) {VI.  $e->gettrace ();    Get the complete error data VII.   $e->getfile ();   Get error file VIII.   $e->getline ();    Gets the error line number IX. $e->getmessAge ();    Gets the cause of the error x.} h) Reflection: I. Reflection is the reaction of the structure of other classes so that the structure of the class can be understood to facilitate the use of the class Ii.reflectioniii.reflectionclass::export (the class name to reflect)  Iv. calling Reflectionclass static method V.var_dump (Reflectionclass::export (' PDO ')); vivii. $RC = new Reflectionclass (' PDO ');  Create Reflectionclass Object Viii.var_dump ($RC->getproperties ());   Get all Properties Ix.var_dump ($RC->getmothods ());  Get all Methods X.var_dump ($RC->getconstants ()); Get all constants
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.