设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1185|回复: 4

Developing MySQL Database Applications With PHP Part 3

  [复制链接]
发表于 2011-8-4 15:05:01 | 显示全部楼层 |阅读模式
Using the PDO Extension With MySQL Driver, pdo_mysql
This series of tutorials shows the essential steps involved in installing PHP with MySQL extensions mysql, mysqli, and pdo_mysql, and with support for the MySQL native driver for PHP, mysqlnd. The tutorials also provide simple examples to connect, insert, and retrieve data from a MySQL database. Because the focus is on database connectivity from a PHP script, these tutorials assume that some kind of MySQL database is already up and accessible from the client machine.

For general information about accessing MySQL with PHP database extensions, a list of the major PHP API offerings, and instructions on setting up the test database used in these tutorials, see the first sections of the first tutorial in this series, Developing MySQL Database Applications With PHP Part 1: Using the MySQL Improved Extension, mysqli.

Application developers who are new to the development of MySQL database applications with PHP are the target audience of this article.

Contents



Use the PHP Data Objects (PDO) Extension With MySQL Driver, pdo_mysql

The PHP Data Objects extension (PDO) defines a lightweight, consistent interface for accessing a variety of databases from PHP applications. PDO provides a database abstraction layer, meaning regardless of which database you're using, you use the same functions to execute SQL queries and fetch data.

Because PDO is just an abstraction layer, you cannot perform any database functions like inserting a row of data into the table using the PDO extension by itself--you must use a database-specific PDO driver to access a database server from the PHP code.  The database driver that implements the PDO interface exposes the database-specific features as regular extension functions. PDO_MYSQL is a database driver that implements the PDO interface to enable access from PHP to MySQL 3.x, 4.x and 5.x databases. The PDO_MYSQL driver is implemented using the PHP extension framework, and it sits in the layer below PDO that provides MySQL-specific functionality. The PHP programmer calls the PDO API, and then PDO uses the PDO_MYSQL driver to carry out communication with the MySQL server.

PDO is useful when you need to build applications capable of talking to different database systems like MySQL, Oracle, or PostgreSQL.

Although PDO has many advantages like a clean, simple, lightweight, portable object-oriented API, its main disadvantage is that it does not allow you to use all of the features that are available in the latest versions of MySQL. For example, the PDO API does not support mixing SQL statements that return result sets with the SQL statements that do not return any data. MySQL allows you to mix all kinds of queries in one call when using the Multiple Statements feature. Using ext/mysqli, you can execute such a query and handle its results properly. However with PDO you cannot. See the first tutorial in this series, Developing MySQL Database Applications With PHP Part 1: Using the MySQL Improved Extension, mysqli, for more information about mysqli.

Install PHP With the PDO Extension and the PDO_MySQL Driver, pdo_mysql


  • PHP 5.1 and later on UNIX / Linux systems

    PDO comes packaged with PHP 5.1 and later versions by default, so if you're running 5.1 or later, you do not need any additional steps to enable PDO extension. It is recommended that you build PDO as a shared extension from the PHP source code, because this will allow you to take advantage of updates that are made available via PECL. The recommended configure line for building PHP with PDO support should enable zlib support (for the pecl installer) as well. Also you need to enable the PDO driver for the MySQL database. If you build PDO as a shared extension, you must build the PDO database driver as a shared extension. On UNIX/Linux systems, a typical configuration line looks like this:

    bash# ./configure --with-zlib --enable-pdo=shared --with-pdo-mysql=shared --with-mysql=shared [other options]


    Then the PDO extension with the PDO_MySQL driver relies on the MySQL client library, libmysql, to talk to the MySQL database server.

    Alternatively, if you wish to use the MySQL native driver for PHP (mysqlnd) in place of the MySQL client library (libmysql) to communicate with the MySQL server, check the installation instructions in the tutorial Developing MySQL Database Applications With PHP Part 4: Using the MySQL Native Driver for PHP, mysqlnd.

    After installing PDO as a shared module, edit your php.ini file to enable the PDO extension and the MySQL driver. Make sure that the MySQL driver (pdo_mysql.so) is listed after the pdo.so, as PDO must be initialized before the database-specific extensions are loaded.
    1. bash# cat php.ini
    2. ...
    3. extension=pdo.so
    4. extension="pdo_mysql.so"
    5. extension="mysql.so"
    6. ...
    复制代码

    Having PDO as a shared module allows you to upgrade the extension by running the simple command: pecl upgrade pdo. This step does not force you to rebuild the whole PHP from scratch though you must upgrade the PDO MySQL driver as well at the same time.
  • PHP 5.0 and later until 5.1 on UNIX/Linux systems

    PDO is available as a PECL extension. Installation can be performed with the help of the pecl tool. You should ensure that PHP is configured --with-zlib in order for pecl to be able to handle the compressed package files.

    Run the following command to download, build, and install the latest stable version of PDO.

    bash# pecl install pdo


    The pecl command automatically installs the PDO module into your PHP extensions directory. To enable the PDO extension on UNIX/Linux operating systems, add the following line to php.ini:

        extension=pdo.so

  • Users of all other OS platforms, check the PDO installation documentation at PHP.net.
  • Finally, verify that PHP can load the PDO extension module and pdo_mysql database driver.

    1. bash# php -m | grep -i pdo
    2. PDO
    3. pdo_mysql
    4. ..
    复制代码

    To display the available PDO database drivers, call the function pdo_drivers().

    For example:
    1. <?php
    2.     print_r(pdo_drivers());
    3. ?>
    复制代码




 楼主| 发表于 2011-8-4 15:09:50 | 显示全部楼层
Test the MySQL Database Connectivity Using the PDO Extension


The following sample PHP script shows how to connect to a MySQL database running on the same host, using the PDO interface. The code sample connects to the MySQL database test by using the PDO API, sends out a query to retrieve all the rows from the table City, extracts the data from the result set, displays the data on the standard output, inserts a couple of new records into the City table using prepared statements, and finally commits the INSERT transactions. To minimize the dependencies, this script is shown as a stand-alone script rather than a PHP page that can be accessed over the web server.
The sample code is provided for the purpose of demonstration only; it is not meant to indicate that readers should adopt a particular style of coding. To keep it simple, the sample code assumes that the user always provides well-formed input, hence there is no explicit error checking code in the following example. Use discretion in reusing the sample code.
  1. bash# cat PHPpdoClient.php

  2. <?php

  3.     $dsn='mysql:host=localhost;port=3306;dbname=test';
  4.     $user='root';
  5.     $pass='admin';

  6.     try {

  7.         /* obtain a database connection handle */
  8.         $dbh = new PDO($dsn, $user, $pass);

  9.     } catch (PDOException $exception) {
  10.             printf("Failed to connect to the  database. Error: %s",  $exception->getMessage());
  11.     }

  12.     $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  13.     $dbh->setAttribute(PDO::ATTR_AUTOCOMMIT, false);

  14.     $sql = "SELECT * FROM City";

  15.     /* execute the query */
  16.     $rs = $dbh->query($sql);

  17.     print "\nRetrieved " . $rs->rowCount() . " row(s).\n";
  18.     print "\nCityName\n--------\n";

  19.     /* retrieve the data from the result set */
  20.     $rs->setFetchMode(PDO::FETCH_ASSOC);

  21.     while ($row = $rs->fetch()) {
  22.         $data = $row["CityName"];
  23.         print "$data\n";
  24.     }

  25.     /* close the result set */
  26.     $rs->closeCursor();

  27.     try {

  28.         /* create a prepared statement */
  29.         $query = "INSERT INTO City VALUES (?)";
  30.         $stmt = $dbh->prepare($query);

  31.         $city = 'London, UK';

  32.         /* bind the parameter */
  33.         $stmt->bindParam(1, $city);

  34.         $dbh->beginTransaction();

  35.         /* execute the SQL */
  36.         if ($stmt->execute()) {
  37.             $dbh->commit();
  38.             echo "\nSuccessfuly inserted " . $city . " into the table, City.";
  39.         } else {
  40.             print_r($dbh->errorInfo());
  41.         }

  42.         $city = 'Paris, France';

  43.         /* bind the parameter with another value */
  44.         $stmt->bindParam(1, $city);

  45.         $dbh->beginTransaction();

  46.          /* execute the SQL again */
  47.          if ($stmt->execute()) {
  48.                  $dbh->commit();
  49.                  echo "\nSuccessfuly inserted " . $city . " into the table, City.\n";
  50.                 } else {
  51.                         print_r($dbh->errorInfo());
  52.                 }

  53.         $sql = "SELECT * FROM City";

  54.         /* execute the query */
  55.         $rs = $dbh->query($sql);

  56.         print "\nRetrieved " . $rs->rowCount() . " row(s).\n";
  57.         print "\nCityName\n--------\n";

  58.         /* retrieve the data from the result set */
  59.         $rs->setFetchMode(PDO::FETCH_OBJ);

  60.         while ($row = $rs->fetch()) {
  61.             $data = $row->CityName;
  62.             print "$data\n";
  63.         }

  64.         /* close the result set */
  65.         $rs->closeCursor();

  66.     } catch (PDOException $exception) {
  67.         print "\nException: " . $exception->getMessage();
  68.         $dbh->rollBack();
  69.     }

  70.     /* close the database connection */
  71.     $dbh = null;

  72. ?>

  73. bash# php PHPpdoClient.php

  74. Retrieved 3 row(s).

  75. CityName
  76. --------
  77. Hyderabad, India
  78. San Francisco, USA
  79. Sydney, Australia

  80. Successfuly inserted London, UK into the table, City.
  81. Successfuly inserted Paris, France into the table, City.

  82. Retrieved 5 row(s).

  83. CityName
  84. --------
  85. Hyderabad, India
  86. San Francisco, USA
  87. Sydney, Australia
  88. London, UK
  89. Paris, France
复制代码

Some of the important steps in the above code sample are explained below.

Make a Connection to the Database


Before interacting with a database using PDO, the first thing to do is to create a PDO object to use as the database handle. This is accomplished by calling PDO's constructor.

        PDO::__construct (string $dsn [, string $username [, string $password [, array $driver_options]]])


where: $dsn is the Data Source Name, which is composed of the following elements: the name of the PDO database driver followed by a colon, followed by optional driver-specific information. In the case of the PDO_MYSQL driver, the DSN prefix is "mysql:", and the PDO mysql driver accepts host, port, dbname and unix_socket as the optional parameters. Note that host or port and unix_socket are mutually exclusive, and hence should not be mixed in the same DSN.

The following statement in the sample PHP script attempts to connect to a MySQL server running on the localhost on the default port 3306 with the credentials root/admin, and selects the test database to perform the subsequent transactions against.

$dbh = new PDO('mysql:host=localhost;port=3306;dbname=test', 'root', 'admin');


The above statement can be rewritten as shown below with the assumption that /tmp/dbconnect exists with appropriate file permissions that enable PHP to read the file.
  1. $dbh = new PDO('uri:file:///tmp/dbconnect', 'root', 'admin');

  2. bash# cat /tmp/dbconnect
  3. mysql:host=localhost;port=3306;dbname=test;
复制代码

If the host is localhost, then the port number is ignored and 3306 is assumed by default. If the MySQL server is running on any port other than the default port 3306, use the IP address 127.0.0.1 in place of localhost in the host argument.

If you wish to use persistent connections, you must set the attribute PDO::ATTR_PERSISTENT in the array of driver options passed to the PDO constructor. The database driver does not use persistent connections if this attribute has been set with PDO::setAttribute() after instantiating the object.

For example:

$dbh = new PDO('mysql:host=localhost;port=3306;dbname=test', 'root', 'admin', array(PDO::ATTR_PERSISTENT => true));


PDO::__construct() throws a PDOException if the attempt to connect to the requested database fails.



 楼主| 发表于 2011-8-4 15:10:36 | 显示全部楼层
Use PDO's Connection-Specific Attributes
  

There are several connection-related options for PDO that you can consider passing to the $driver_options array. Alternatively, you can set those options against the database connection handle by using the PDO::setAttribute().
The following statement in the sample script sets the PDO error reporting mode to PDO_ERRMODE_EXCEPTION, which enables PHP to throw exceptions similar to that of other programming languages. An exception can be thrown and caught within PHP. Code can be surrounded in a try block to facilitate the catching of potential exceptions.

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


The following statement disables the default auto-commit behavior of MySQL.

$dbh->setAttribute(PDO::ATTR_AUTOCOMMIT, false);


To enable buffered queries by default, set the attribute PDO::MYSQL_ATTR_USE_BUFFERED_QUERY to TRUE as shown below. Buffered queries gives you the ability to work with the entire result set rather than one row at a time, or to move to any record of your choice randomly.

$dbh->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);


Check the PDO_MYSQL driver documentation for MySQL driver-specific constants.

Execute the SQL Statement
  

The PDO::query() method executes an SQL statement and returns a result set that can be retrieved using any of the PDOStatement::fetchXX() methods.

The following code fragment in the sample PHP script executes the SQL query.
  1. $sql = "SELECT * FROM City";
  2. $rs = $dbh->query($sql);
复制代码

When executing queries that do not return a result set such as INSERT, DELETE, and UPDATE, use the PDO::exec() method. The exec() method returns the number of rows that were modified or deleted by the SQL statement. If no rows were affected, exec() returns 0.

If an SQL statement has to be executed multiple times, doing so is more efficient with prepared statements. (For a discussion of prepared statements, see the first tutorial in this series, Developing MySQL Database Applications With PHP Part 1: Using the MySQL Improved Extension, mysqli.) In order to use prepared statements, prepare a PDOStatement object with PDO::prepare(), and then execute the SQL statement with PDOStatement::execute().

The following code fragment in the sample PHP script inserts a new record into the City table, using a prepared statement.
  1. $query = "INSERT INTO City VALUES (?)";
  2. $stmt = $dbh->prepare($query);

  3. $city = 'London, UK';

  4. $stmt->bindParam(1, $city);
  5. $stmt->execute();
复制代码

PDO has no direct support for MySQL's Multiple Statements.

Transaction Support in PDO


When working with the PDO extension, auto-commit mode is enabled by default. In the auto-commit mode, the database driver implicitly commits every successful update to the database. Note that MySQL also automatically issues an implicit COMMIT when a DDL statement such as CREATE TABLE or DROP TABLE is issued within a transaction. The implicit COMMIT prevents you from rolling back any other changes within the transaction boundary.

To change the default auto=commit behaivor, use the transaction support in PDO. Calling PDO::beginTransaction() initiates a transaction and turns off the auto-commit mode. While auto-commit mode is turned off, changes made to the database via the PDO object instance are not committed until you terminate the transaction by calling PDO::commit(). Calling PDO::rollBack() rolls back all changes to the database and returns the connection to the auto-commit mode.

If the PHP script exits unexpectedly, or when you close a database handle with pending transactions, PDO calls the rollBack() method automatically on all the pending transactions to reduce the risk of committing the incomplete transactions to the database.

The following code fragment in the sample PHP script initiates a transaction, issues an INSERT statement to add a new record to the database, and finally commits the transaction to the database based on the return value of PDO::exec(). In the case of an exception, the pending INSERT gets rolled back.
  1. try {

  2.     $query = "INSERT INTO City VALUES (?)";
  3.     $stmt = $dbh->prepare($query);

  4.     $city = 'London, UK';

  5.     $stmt->bindParam(1, $city);

  6.     $dbh->beginTransaction();

  7.     if ($stmt->execute()) {
  8.         $dbh->commit();
  9.     } else {
  10.         print_r($dbh->errorInfo());
  11.     }

  12. }  catch (PDOException $exception) {

  13.     print "\nException: " . $exception->getMessage();
  14.     $dbh->rollBack();

  15. }

复制代码

Retrieve the Data From the Result Set
  

PDO's data retrieval methodology is quite similar to that of other extensions like mysqli and mysql. ThePDOStatement class has four different fetchXX() methods to retrieve the data from the result set. The fetch() method fetches the next row from a result set, the fetchAll() method returns an array containing all of the result set rows, the fetchObject() method fetches the next row and returns it as an object, and the final method fetchColumn() returns a single column from the next row of a result set.

Before calling the fetch() and fetchAll() methods, you can set the fetch style using PDOStatement->setFetchMode() to control how the rows in the result set are returned to the caller. It is also possible to set the fetch style by setting one of the optional input parameters to the fetch() and fetchAll() methods. Check the documentation of the PDOStatement->fetch() method for the supported fetch styles.

The following code fragments in the sample PHP script used PDO::FETCH_ASSOC and PDO::FETCH_OBJ fetch styles to retrieve the result set. The PDO::FETCH_ASSOC method returns the result set as an associative array indexed by column name, and PDO::FETCH_OBJ returns an anonymous object with property names that correspond to the column names returned in the result set.
  1. ...

  2. $rs->setFetchMode(PDO::FETCH_ASSOC);

  3. while ($row = $rs->fetch()) {
  4.     $data = $row["CityName"];
  5.     print "$data\n";
  6. }

  7. ...
  8. ...

  9. $rs->setFetchMode(PDO::FETCH_OBJ);

  10. while ($row = $rs->fetch()) {
  11.     $data = $row->CityName;
  12.     print "$data\n";
  13. }

  14. ...
复制代码

Alternatively, you can retrieve all the rows in the result set with a single call to the PDOStatement::fetchAll() method.
Once the required data has been retrieved from the result set, it is a recommended practice to free up the connection to the database server so that further queries can be executed. Use PDOStatement::closeCursor() to accomplish this task. The closeCursor() method is useful for database drivers that do not support executing a PDOStatement object when a previously executed PDOStatement object still has unfetched rows.

The following code fragment in the sample PHP script closes the result set cursor.
  1. $rs = $dbh->query($sql);
  2. $row = $rs->fetch();
  3. $rs->closeCursor();
复制代码

Close the Database Connection
  

When you are finished with the database, close the MySQL database connection by assigning a NULL value to the PDO object.

$dbh = null;


It is not always necessary to close a connection when you are finished, but it is necessary to close the connection to the database if you want to open up a new connection to a different database.


 楼主| 发表于 2011-8-4 15:10:54 | 显示全部楼层
Explore Other Tutorials in This Series


To find out more about using MySQL with PHP, see all tutorials in the series:

For More Information




发表于 2011-11-17 09:29:06 | 显示全部楼层
加油啊!!!!顶哦!!!!!













发表于 2011-12-13 12:29:33
继续关注一下这方面的信息












北京商标代理
支持 反对

发表于 2012-1-1 02:23:29
真是好东西呀












商标注册加急办理
支持 反对

发表于 2012-1-21 09:12:26

Apple MacBook Air Fall 2010| SN025

beijing,dell inspiron 14r, jan. 18 ,sony vaio vpcf13ufxh, according to hong kong,nikon 18-200mm lens, because the body can produce hydrogen and methane,lenovo ideapad u350, if such flammable gas accumulation in a confined environment ,toshiba satellite m645-s4118x, may lead to fire . in other words ,asus b53f-a1b, astronauts in space wearing a spacesuit at the cabin or fart or burp , at any time may lead to a huge explosion.

nutrition experts kaluo wei and murphy analyze the space consumption of new light meal of gas exhaled by people and rectal gas ,hp elitebook 2540p xt931ut, and with the 1960s for comparison,lenovo ideapad y560, in order to understand the accumulation of hydrogen and methane emissions and impact on the work of space .

study found that those who tested different gases emitted ,canon rebel t2i, the new space meal who can reduce emissions from gas consumption . according to reports,hp elitebook 8560p xu063ut, large intestine,sony vaio vpcf132fx/b, stomach gas is generated when bacteria break down food ,apple macbook air fall 2010, and with hydrogen ,nikon d5000, methane,hp eb 8460p, nitrogen ,sony vaio vpcf132fx/b, carbon dioxide,dell nirvana 3450, oxygen and hydrogen sulfide . normal human daily fart about 12 to 40 can be discharged from 0.5 to 2 liters gas . since plants can not be digested cellulose ,sony hxr-mc50u, vegetarians than omnivores gas emissions to be more .

share : welcome to comment to comment
microblogging recommended | today's hot microblogging ( edit : sn025)
statement: sina posted this article for transferring more information purpose ,asus b53j-d1b, does not mean that agree with their views or confirm the description .
related articles:

  
   hp dv6-6090us|
  
   [/url]
  
   [url=http://www.blog2net.com/blog.php?user=bestv88v&note=428780]toshiba m11| sn014

  
   [/url]
  
   [url=http://www.zgd2w.com/bbs/viewthread.php?tid=1460164&pid=2351039&page=82&extra=page%3d1#pid2351039]asus k42f-b1| wang
支持 反对

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2026-2-5 16:31 , Processed in 0.016316 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表