PHP operation MySQL Method:
Mysql
Mysqli
Pdo
Database Abstraction Layer--pdo (emphasis)
=======================================================================
1. The role of PDO
The PDO (PHP data Object) Extension class library provides a lightweight, unified interface for PHP access to the database. Whether you use
Database, you can execute queries and get data through consistent functions
Advantage:
① provides a unified approach to different databases
② is highly efficient in performing bulk SQL and optimizes PHP operational SQL
③PDO provides a preprocessing mechanism to improve security against SQL injection
Disadvantage:
Difficult to learn
Mysqli and PDO comparison
① processing massive data PDO high efficiency
② Multi-concurrency operation PDO High efficiency
2. Installation of PDO (directly over)
①php.ini Open Extension=php_pdo.dll Extension=php_pdo_mysql.dll
② Restart Apache
3. Create a PDO object
3.1 Calling construction methods in several ways
Dsn:data Source name database type host dbname
① incoming DSN USER pass directly in the constructor method
Mysql:host=localhost;dbname=s44
② is defined in the php.ini [PDO]:
Look again.
③ Write configuration file definition constants
To write a parameter into a constant dbconfig.php
3.2 PDO links-related options
Set the value of the connection data: SetAttribute (Attrname/attrnum,attrname/attrnum);
Gets the value of the Connection property (two ways): GetAttribute () (recommended) or the new PDO fourth parameter
① Error Handling method Pdo::attr_errmode
② whether to submit pdo::attr_autocommit automatically
③ result set Array mode Pdo::attr_default_fetch_mode
3.3 PDO's character set settings
$pdo->exec (' Set names UTF8 ');
Or
$pdo->query (' Set names UTF8 ');
The error handling mode for 3.3 PDO is for the following SQL execution section
Set Pdo::attr_errmode
Value: Pdo::errmode_silent no error (default)
Value: Pdo::errmode_warning warning
Value: Pdo::errmode_exception exception (recommended)
Echo ' default error mode value is: '. $pdo->getattribute (pdo::attr_errmode). ' <BR> '. ' <br> ';//The default error mode value is: 0
The value of ECHO ' no error mode is: '. Pdo::errmode_silent. ' <BR> '. ' <br> ';//The value of no error mode is: 0
The value of ECHO ' warning error mode is: '. Pdo::errmode_warning. ' <BR> '. ' <br> ';//warning the value of the error mode is: 1
The value of ECHO ' exception mode mode is: '. Pdo::errmode_exception. ' <BR> '. ' <br> ';//The value of the exception mode mode is: 2
3.4 A member method in a PDO object
Query ()//Perform a lookup operation
EXEC ()//Perform write operation
SetAttribute ()//Set connection properties
GetAttribute ()//Get connection properties
Lastinsertid ()//get last Insert ID
4. Pdostatment objects
4.1 Member methods in Pdostatmen
Fetch
Fetchall
4.2 Set the array type that returns the result
Pdo::fetch_assoc
Pdo::fetch_num
Pdo::fetch_both
Pdo::fetch_objstdclass padding Properties
The default mode for iterating through an array is Both:3
Echo ' default traversal array pattern is BOTH: '. $pdo->getattribute (pdo::attr_default_fetch_mode). ' <BR> '. ' <BR> ';
The assoc value of ECHO ' associative array pattern is '. Pdo::fetch_assoc. ' <BR> '. <BR> ';//The Assoc value of the associative array pattern is 2
ECHO ' indexed array pattern num's value is: '. Pdo::fetch_num. ' <BR> '. <BR> ';//index array mode num value is: 3
ECHO ' Both value for all modes is: '. Pdo::fetch_both. ' <BR> '. ' <BR> ';//The both value for all modes is: 4
The value of ECHO ' object mode obj is: '. Pdo::fetch_obj. ' <BR> '. <BR> ';//The value of object mode obj is: 5
4.3 PDO processing result set mode
1. Fetch traversal
2, Fetchall All Access
3. Traverse $stmt Object directly
5. Use of PDO pretreatment (emphasis)
5.1 Advantages of preprocessing
Prevent SQL injection
Batch processing can improve SQL processing efficiency
5.2 Pre-processing steps
1. Prepare SQL with placeholders instead of conditional parameters (?). /: Name)
2. Perform preprocessing prepare return Pdostatement object
3. Execute binding Parameters
4. Execute SQL
5.3 Preprocessing SQL methods
5.4 How to bind parameters
5.5 Binding the results of a query
6. PDO transaction mechanism
6.1 MySQL Transaction
6.2MYSQL Client operation Process:
Using transactions in 6.3 PDO
Object-oriented Seventh day----Database Abstraction Layer PDO