PDO is a significant feature of new PHP 5, since php4/php3 before PHP 5 is a bunch of database extensions to connect and process with each database, what Php_mysql.dll, Php_pgsql.dll, Php_mssql.dll, Php_ Sqlite.dll and so on extension to connect MySQL, PostgreSQL, MS SQL Server, SQLite, similarly, we have to use ADOdb, PEAR::D B, Phplib::D B, such as database abstract class to help us, extremely cumbersome and inefficient , after all, how efficient is the PHP code that we write directly in C + + with the extended slope high pinch? So, the appearance of PDO is inevitable, we have to calm learning attitude to accept the use, perhaps you will find that you can reduce a lot of kung fu Oh.
Installing PDO
I am on Windows XP SP2 above, so, the whole process is on the Windows line, as for the LINUX/FREEBSD platform, please find the data set installation.
My is PHP 5.1.4, already has the extension of the Php_pdo.dll, but need a little set up to use.
Open C:\windows\php.ini, that is my php config file, find the following line:
Extension_dir
This is the directory where our extension exists, and my PHP 5 extension is in: C:\php5\ext, then I will change this line to:
Copy CodeThe code is as follows:
Extension_dir = "C:/php5/ext"
Then go to php.ini below to find:
Copy CodeThe code is as follows:
; Dynamic Extensions;
Here's a bunch of things like, Extension=php_mbstring.dll, here's the PHP extension load configuration, and we'll add our PDO extensions on the last side:
Extension=php_pdo.dll
Extension=php_pdo_mysql.dll
Extension=php_pdo_pgsql.dll
Extension=php_pdo_sqlite.dll
Extension=php_pdo_mssql.dll
Extension=php_pdo_odbc.dll
Extension=php_pdo_firebird.dll
; Extension=php_pdo_oci8.dll
A variety of PDO driver, can give plus, but the back of the Php_pdo_oci8.dll, because I did not install the Oralce database, so without this, use a semicolon to comment out it. Then restart our web server, Iis/apache, my is IIS, hehe, table despise me, on windows, simple. After rebooting, write a phpinfo.php file in our Web server's documentation directory, plus these:
Copy CodeThe code is as follows:
Phpinfo ();
?>
The output of the content, if you can see smoothly:
Pdo
PDO Support Enabled
PDO drivers MySQL, Pgsql, SQLite, MSSQL, ODBC, Firebird
There are various driver instructions in the rear: Pdo_firebird,pdo_mssql,pdo_mysql,pdo_odbc,pdo_pgsql,pdo_sqlite. Well, congratulations on your installation, otherwise please check the above steps carefully.
PDO Tutorials
I use MySQL 4.0.26, but I personally recommend that you use MySQL 4.1.x or MySQL 5.0.x, because those editions have a lot of interesting things worth learning. We here PDO need to connect is my MySQL 4.0, if you do not install MySQL, please install yourself. We built MySQL and added table foo to the test library, including four fields such as Id,name,gender,time.
We started constructing the first PDO application, creating a pdo.php file under the Web document directory:
Copy CodeThe code is as follows:
$DSN = "Mysql:host=localhost;dbname=test";
$db = new PDO ($dsn, ' root ', ');
$count = $db->exec ("INSERT into foo SET name = ' Heiyeluren ', gender= ' man ', Time=now ()");
Echo $count;
$DB = null;
?>
$DSN = "Mysql:host=localhost;dbname=test";
is to construct our DSN (data source) and look at the information included: the database type is MySQL, the host address is localhost, the database name is test, just a few messages. Data sources in different databases are constructed differently.
$db = new PDO ($dsn, ' root ', ');
Initialize a PDO object, the parameters of the constructor the first one is our data source, the second is the user who connects to the database server, and the third parameter is the password. We cannot guarantee that the connection is successful, we will talk about the exception later, and here we would consider it to be a successful connection.
$count = $db->exec ("INSERT into foo SET name = ' Heiyeluren ', gender= ' man ', Time=now ()");
Echo $count;
Call our successful PDO object to execute a query that is an operation that inserts a record, and uses the Pdo::exec () method to return a result that affects the record, so we output this result. Finally, you need to end the object resource:
$DB = null;
The default is not a long connection, if you need a long database connection, you need to add one last parameter: Array (pdo::attr_persistent = True) becomes this:
$db = new PDO ($dsn, ' root ', ' ', array (pdo::attr_persistent = true));
One operation is so simple, perhaps not much different from the previous, and ADODB is somewhat similar.
PDO step-by-step tutorial
If we want to extract the data, then we should use the data acquisition function. (The $db used below are all connected objects above)
Copy CodeThe code is as follows:
foreach ($db->query ("SELECT * from foo")) {
Print_r ($row);
}
?>
We can also use this access method:
Copy CodeThe code is as follows:
$rs = $db->query ("SELECT * from foo");
while ($row = $rs->fetch ()) {
Print_r ($row);
}
?>
If you want to get the data in the array at once, you can:
Copy CodeThe code is as follows:
$rs = $db->query ("SELECT * from foo");
$result _arr = $rs->fetchall ();
Print_r ($result _arr);
?>
The results of the output are as follows:
Copy CodeThe code is as follows:
Array
([0] = = Array (
[id] = 1
[0] = 1
[Name] = Heiyeluren
[1] = Heiyeluren
[Gender] = men
[2] = = Male
[Time] = 2006-10-28 23:14:23
[3] = 2006-10-28 23:14:23
)
}
We look at the records inside, the digital index and the associated index all have, waste resources, we just need to correlate the index:
Copy CodeThe code is as follows:
$db->setattribute (Pdo::attr_case, Pdo::case_upper);
$rs = $db->query ("SELECT * from foo");
$rs->setfetchmode (PDO::FETCH_ASSOC);
$result _arr = $rs->fetchall ();
Print_r ($result _arr);
?>
Look at the above code, the SetAttribute () method is to set some properties, the main properties are: Pdo::attr_case, Pdo::attr_errmode and so on, we need to set the pdo::attr_case here, is when we use the associated index to get the data set, whether the associated index is uppercase or lowercase, there are several choices:
Pdo::case_lower--Force column names to be lowercase
Pdo::case_natural--column names are in the original way
Pdo::case_upper--Force column name to uppercase
We use the Setfetchmode method to set the type that gets the return value of the result set, and the same type:
PDO::FETCH_ASSOC--Associative array form
Pdo::fetch_num--Digital Index array form
Pdo::fetch_both--both array form, this is the default
Pdo::fetch_obj-In the form of an object, similar to the previous mysql_fetch_object ()
Of course, in general we are using PDO::FETCH_ASSOC, specifically what to use, follow your own needs, other get type reference manuals.
In addition to the above method of obtaining data, there is this:
Copy CodeThe code is as follows:
$rs = $db->prepare ("SELECT * from foo");
$rs->execute ();
while ($row = $rs->fetch ()) {
Print_r ($row);
}
?>
Actually, almost. If you want to get a result from a field in the specified record, you can use Pdostatement::fetchcolumn ():
Copy CodeThe code is as follows:
$rs = $db->query ("Select COUNT (*) from foo");
$col = $rs->fetchcolumn ();
Echo $col;
?>
It is common to use Fetchcolumn () for Count statistics or some records that require only a single field to operate well.
A simple summary
The query operation is mainly Pdo::query (), Pdo::exec (), PDO::p repare (). Pdo::query () is primarily used for operations that have logged results returned, particularly the select operation, where Pdo::exec () is primarily for operations that do not have a result set, such as INSERT, UPDATE, delete, and so on, which returns the number of columns affected by the current operation. PDO::p repare () is mainly pre-processing operations, need to $rs->execute () to perform preprocessing inside the SQL statement, this method can bind parameters, the function is relatively strong, not this article can be simple to understand, you can refer to manuals and other documents. The main operations for getting result sets are: Pdostatement::fetchcolumn (), Pdostatement::fetch (), Pdostatement::fetchall (). Pdostatement::fetchcolumn () is a field that gets the result that specifies the first record, and the default is the first field. Pdostatement::fetch () is used to get a record, Pdostatement::fetchall () is to get all the recordset into one, and get the result can be pdostatement by: Setfetchmode to set the type that requires the result collection.
There are also two peripheral operations, one of which is Pdo::lastinsertid () and Pdostatement::rowcount (). Pdo::lastinsertid () is the last insert operation returned, and the primary key column type is the final self-increment ID. Pdostatement::rowcount () is primarily used for pdo::query () and PDO::p Repare () for the result set affected by the delete, INSERT, update operation, for Pdo::exec () Method and select operation are not valid.
As seen from the above, the PDO function is really powerful, and there are some things I have not mentioned, such as binding parameters, preprocessing, stored procedures, transaction processing and so on. There are different data extension DSN constructs, Oracle database own a lot of special things, need to go deep to learn to understand, this article simply describes some of the introductory knowledge, is a simple understanding of PDO.
http://www.bkjia.com/PHPjc/324301.html www.bkjia.com true http://www.bkjia.com/PHPjc/324301.html techarticle PDO is a major feature of new PHP 5, since php4/php3 before PHP 5 is a bunch of database extensions to connect and process with each database, what Php_mysql.dll, Php_pgsql ...