oop007 Review
2014-9-4 9:42:28
Summary:
1, the role of PDO 2, PDO installation 3, PDO Connection property settings 4, PDO object and Pdostatement object 5, PDO preprocessing 6, PDO transaction mechanism
1, the role of PDO
Brief introduction:
The PDO (php data Object) Extension class library defines a lightweight, consistent interface for the PHP Access database, which provides a database access abstraction layer.
This way, no matter what database you use, you can execute queries and get data through consistent functions.
PDO greatly simplifies the operation of the database and can mask differences between different databases.
Using PDO is a convenient way to develop cross-database programs and migrate between different databases, which is the main development direction of PHP in the future database processing.
It can support a variety of databases such as Mysql,postgresql,oracle,mssql.
To illustrate:
If we do a project that uses MySQL, and if it's a process-oriented database, basically all of the database functions are in mysql_ form.
Now we suddenly have to change the database, such as pgsql. Pgsql database functions are basically pg_ forms.
And the two parts of the function do not exist with each other, have their own "extension function."
Then it is more complicated to change the project.
But if we are using the PDO operations database, everything is simple.
With MySQL, we only need to define this when we connect:
$m=new PDO ("Mysql:host=localhost;dbname=test", "root", "123");
With Pgsql, we only need to define this when we connect:
$m=new PDO ("Pgsql:host=localhost;port=5432;dbname=test", "Postgres", "123");
Other operations are the same, such as the query is $stmt= $m->query ($sql), traversal is $stmt->fetchall ();
Everything has become easier. PDO also provides preprocessing and transactional support.
2, the installation of PDO
[Windows Environment]
01) Edit the php.ini file:
Extension=php_pdo. dllextension=php_pdo_mysql.dll
02) Restart Apache Service:
Httpd–k restart
03) Open phpinfo.php to see if there are PDO, Pdo_mysql
[Linux Environment (CentOS6.0)]
The general steps are:
1 tar -zxvf pdo_mysql-1.0. 2 . tgz 2 CD pdo_mysql-1.0. 2 3 /usr/local/php/bin/phpize4 ./configure--with-php-config=/usr/local/php/bin/ Php-config--with-pdo-mysql=/usr/local/mysql5make6 make Install
Edit PHP.ini, add
Note: $ext _dir is the extension directory name, for example no-debug-zts-20060613
1 extension_dir=/usr/local/php/lib/php/extensions/$ext _dir2 extension=pdo_mysql.so
Then restart the server.
3. PDO Connection Property settings
* Connection Database format:
string $dsn string $username string $password Array $driver _options ]] )
01) Connect MySQL
$m=new PDO ("Mysql:host=localhost;dbname=test", "root", "123");
02) Connection Pgsql
$m=new PDO ("Pgsql:host=localhost;port=5432;dbname=test", "Postgres", "123");
03) Connect Oracle
$m=new
* However, it is common to connect using exception handling, such as:
1 Try {2$m=new PDO ("Mysql:host=localhost;dbname=test", "root", "123"); 3 }catch$e) {4die$e-GetMessage () ); 5 }
*PDO Connection-related options
PDO::attr_errmodepdo:: errmode_silent 0 Ignore error mode PDO:: errmode_warning 1 warning level mode PDO :: Errmode_exception 2 exception handling mode PDO::attr_autocommit // turn off auto-commit // turn on auto-commit PDO::attr_default_fetch_mode PDO:: Fetch_assoc 2PDO:: Fetch_num 3PDO:: Fetch_both 4 PDO:: Fetch_obj 5
For example:
1 $option=array(pdo::attr_errmode=>pdo::errmode_exception); 2 $m=new PDO ("Mysql:host=localhost;dbname=test", "root", "123",$option);
Or a more general way to set properties:
1 $m->setattribute (Pdo::attr_errmode, pdo::errmode_exception); // set the exception handling method 2 $m->setattribute (Pdo::attr_default_fetch_mode, PDO::FETCH_ASSOC); // set default associated index traversal
Common PDO attribute output:
1 Echo"\npdo whether to turn off autocommit feature:".$m->getattribute (PDO::attr_autocommit);2 Echo"\ nthe error handling mode for the current PDO:".$m->getattribute (PDO::Attr_errmode); 3 Echo"\ nthe case conversion for table field characters:".$m->getattribute (PDO::attr_case); 4 Echo"\ n Unique information related to connection status:".$m->getattribute (PDO::attr_connection_status); 5 Echo"\ nthe empty string is converted to SQL null:".$m->getattribute (PDO::attr_oracle_nulls); 6 Echo"\ n the application gets the data size in advance:".$m->getattribute (PDO::attr_persistent); 7 Echo"\ n Database-specific server information:".$m->getattribute (PDO::attr_server_info); 8 Echo"\ nthe database server version number information:".$m->getattribute (PDO::attr_server_version);9 Echo"\ nthe database client version number information:".$m->getattribute (pdo::attr_client_version);
* Character Set settings
Set the client string and the connection string set for PHP when connecting to MySQL:
exec, $pdo("Set names UTF8");
Or:
$pdo->query ("Set names UTF8");
4. PDO objects and Pdostatement objects
The member method in the PDO object
1.PDO::begintransaction-start a transaction2.PDO::commit-commit a transaction3.PDO::__construct-Creating a PDO instance that represents a database connection4.PDO::errorcode-Gets the SQLSTATE associated with the last operation of the database handle5.PDO::errorinfo-getting error messages6.PDO::exec-Executes an SQL statement,and returns the number of rows affected7.PDO::getattribute-Retrieving the properties of a database connection*8.pdo::getavailabledrivers-returns an array of available drives (learn about it)*9.pdo::intransaction-Check whether within a transaction (understand)10.PDO::lastinsertid-returns the ID or sequence value of the last inserted row11.PDO::p repare-to create a preprocessing of SQL,returns the Pdostatement object12.pdo::query-is used to execute query SQL statements,returns the Pdostatement object13.PDO::quote-Adding single quotes to a SQL string14.PDO::rollback-rolling back a transaction15.pdo::setattribute-Setting properties
Pdo::query () method
The query () method in the PDO object should be used when executing a select query that returns a result set, or when the number of rows affected does not matter.
If the method executes the specified query successfully, a Pdostatement object is returned.
If you use the query () method and want to know how to get the total number of rows of data, you can use the rowcount () method in the Pdostatement object to get
Pdo::exec () method
The exec () method in the PDO object is used to execute when executing a query that does not have a result set insert,update,delete.
When the method executes successfully, the number of rows affected is returned. Note that this method cannot be used with a select query.
-------------------------------------------------------------------------------------------
Example:
1<?PHP2 Try{3 $m=NewPDO ("Mysql:host=localhost;dbname=test", "root", "123");4}Catch(pdoexception$e){5 die(' Database connection failed: '.$e-getMessage ());6 }7 8 $stmt=$m->query ("Select * from Stu");//returns the Pdostatement object $stmt9 Echo $stmt-RowCount ();Ten?>
-------------------------------------------------------------------------------------------
Pdostatement) member methods in the object
1.pdostatement::bindcolumn-binds a column to a PHP variable (*)2.pdostatement::bindparam-binds a parameter to the specified variable name (*)3.pdostatement::bindvalue-binds a value to a parameter (*)4.PDOStatement::closecursor-closes the cursor so that the statement can be executed again. 5.PDOStatement::columncount-Returns the number of columns in the result set6.PDOStatement::debugdumpparams-Printing a SQL preprocessing command7.pdostatement::errorcode-gets the SQLSTATE associated with the last statement handle operation (*)8.pdostatement::errorinfo-gets the extended error message associated with the last statement handle operation (*)9.pdostatement::execute-executes a preprocessing statement (*)10.pdostatement::fetch-gets the next row from the result set (*)11.pdostatement::fetchall-returns an array containing all the rows in the result set (*)12.PDOStatement::fetchcolumn-Returns a separate column from the next row in the result set. 13.PDOStatement::fetchobject-gets the next line and returns as an object. 14.pdostatement::getattribute-retrieves a statement property (*)15.PDOStatement::getcolumnmeta-Returns the metadata for a column in the result set16.PDOStatement::nextrowset-advance to the next rowset in a multi-rowset statement handle17.pdostatement::rowcount-returns the number of rows affected by the previous SQL statement (*)18.pdostatement::setattribute-set a statement property (*)19.pdostatement::setfetchmode-sets the default fetch mode for the statement.
Note: (*) indicates the method that must be used.
5. PDO pretreatment
Prepares an SQL statement to be executed using the Pdostatement::execute () method.
Pre-processing SQL statements can be labeled with 0 or more named (: Name) or marked with a number (?). form. For example,
1 $stmt=$m->prepare ("INSERT into Stu (NAME,AGE,SEX,CLASSID) VALUES (?,?,?,?)" ); 2 $stmt=$m->prepare ("INSERT into Stu (NAME,AGE,SEX,CLASSID) VALUES (: Name,:age,:sex,:classid)");
The benefit of preprocessing is that you can prevent SQL injection and perform more efficiently to support bulk operations.
* See PDO pretreatment
6. PDO transaction mechanism
Summary: Multiple SQL operations (redaction) as an operating unit, either succeed or fail.
Single data without transactional processing
* See PDO transaction processing
PHP Object-oriented 07