pdo是php5中新加入的数据库抽象层,为了解决访问不同数据库统一接口的问题。类似于PEAR::DB类和ADODB类的操作,不过它是直接封装再php扩展中,可以自由选择使用。 目前的php5.1beta中已经更好的加入了pdo的支持,我们下看具体帮助。 http://cn.php.net/pdo 为了描述清楚,我们把直接把帮助复制过来。 ============================================================================ PDO Functions The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP. Each database driver that implements the PDO interface can expose database-specific features as regular extension functions. Note that you cannot perform any database functions using the PDO extension by itself; you must use a database-specific PDO driver to access a database server. Windows Follow the same steps to install and enable the PDO drivers of your choice. Windows users can download the extension DLL php_pdo.dll as part of the PECL collection binaries from /downloads.php or a more recent version from a PHP 5 PECL Snapshot. To enable the PDO extension on Windows operating systems, you must add the following line to php.ini: Next, choose the other DB specific DLL files and either use dl() to load them at runtime, or enable them in php.ini below pdo_pdo.dll. For example: extension=php_pdo.dll
extension=php_pdo_firebird.dll
extension=php_pdo_mssql.dll
extension=php_pdo_mysql.dll
extension=php_pdo_oci.dll
extension=php_pdo_oci8.dll
extension=php_pdo_odbc.dll
extension=php_pdo_pgsql.dll
extension=php_pdo_sqlite.dll |
These DLL's should exist in the systems extension_dir.
Linux and UNIX Due to a bug in the pear installer you should install the PDO package manually using the following steps: Follow the same steps to install and enable the PDO drivers of your choice. Download the PDO package to your local machine: bash$ wget http://pecl.php.net/get/PDO |
Determine your PHP bin directory. If your PHP 5 CLI binary lives at /usr/local/php5/bin/php then the bin dir is /usr/local/php5/bin. Set your path so that your PHP bin directory is at the front: export PATH="/usr/local/php5/bin:$PATH" |
Manually build and install the PDO extension: bash$ tar xzf PDO-0.2.tgz
bash$ cd PDO-0.2
bash$ phpize
bash$ ./configure
bash$ make
bash$ sudo -s
bash# make install
bash# echo extension=pdo.so >> /usr/local/php5/lib/php.ini |
The following drivers currently implement the PDO interface: Represents a connection between PHP and a database server. beginTransaction - begins a transaction commit - commits a transaction exec - issues an SQL statement and returns the number of affected rows errorCode - retrieves an error code, if any, from the database errorInfo - retrieves an array of error information, if any, from the database getAttribute - retrieves a database connection attribute lastInsertId - retrieves the value of the last row that was inserted into a table prepare - prepares an SQL statement for execution query - issues an SQL statement and returns a result set quote - returns a quoted version of a string for use in SQL statements rollBack - roll back a transaction setAttribute - sets a database connection attribute
Represents a prepared statement and, after the statement is executed, an associated result set. bindColumn - binds a PHP variable to an output column in a result set bindParam - binds a PHP variable to a parameter in the prepared statement columnCount - returns the number of columns in the result set errorCode - retrieves an error code, if any, from the statement errorInfo - retrieves an array of error information, if any, from the statement execute - executes a prepared statement fetch - fetches a row from a result set fetchAll - fetches an array containing all of the rows from a result set fetchSingle - returns the data from the first column in a result set getAttribute - retrieves a PDOStatement attribute getColumnMeta - retrieves metadata for a column in the result set nextRowset - retrieves the next rowset (result set) rowCount - returns the number of rows that were affected by the execution of an SQL statement setAttribute - sets a PDOStatement attribute setFetchMode - sets the fetch mode for a PDOStatement
我们看第一组方法是查询的部分方法,第二组方法是获取数据集的方法。 我们看如何连接一个数据库。我们使用pdo的构造函数来连接数据库: PDO::__construct(no version information, might be only in CVS) PDO::__construct -- Creates a PDO instance representing a connection to a database 说明PDO PDO::__construct ( string dsn [, string username [, string password [, array driver_options]]] ) Creates a PDO instance to represent a connection to the requested database. 参数- dsn
The Data Source Name, or DSN, contains the information required to connect to the database. In general, a DSN consists of the PDO driver name, followed by a colon, followed by the PDO driver-specific connection syntax. Examples of each driver are given below: - PDO_DBLIB
The DSN prefix is either sybase: or mssql: depending on which libraries it was linked against during compilation. sybase:host=localhost; dbname=testdb mssql:host=localhost; dbname=testdb - PDO_FIREBIRD
firebird:User=john;Password=mypass;Database=DATABASE.GDE;DataSource=localhost;Port=3050 - PDO_MYSQL
mysql:host=localhost;dbname=testdb - PDO_OCI
To connect via tnsnames.ora, use: oci:mydb If using instantclient, use: oci:dbname=//localhost:1521/testdb - PDO_ODBC
odbc:DSN=SAMPLE;UID=john;PWD=mypass DSN=SAMPLE refers to the SAMPLE data source configured in the ODBC driver manager. - PDO_PGSQL
pgsql:host=localhost port=5432 dbname=testdb user=john password=mypass Note, by passing user and password in the DSN, the username and password parameters become optional. If specified, they are glued to the end of the connection string. - PDO_SQLITE
sqlite:/path/to/database To create a database in memory, use: sqlite::memory:
The dsn parameter supports three different methods of specifying the arguments required to create a database connection: - Driver invocation
dsn contains the full DSN. - URI invocation
dsn consists of uri: followed by a URI that defines the location of a file containing the DSN string. The URI can specify a local file or a remote URL. uri:file:///path/to/dsnfile - Aliasing
dsn consists of a name name that maps to pdo.dsn.name in php.ini defining the DSN string. 注: The alias must be defined in php.ini, and not .htaccess or httpd.conf
- username
The user name for the DSN string. This parameter is optional for some PDO drivers. - password
The password for the DSN string. This parameter is optional for some PDO drivers. - driver_options
A key=>value array of driver-specific connection options.
返回值Returns a PDO object on success. 异常PDO::construct() throws a PDOException if the attempt to connect to the requested database fails. 例例子 1. Create a PDO instance via driver invocation <?php $dsn = 'mysql:dbname=testdb;host=127.0.0.1'; $user = 'dbuser'; $password = 'dbpass';
try { $dbh = new PDO($dsn, $user, $password); } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); }
?>
|
|
例子 2. Create a PDO instance via URI invocation The following example assumes that the file /usr/local/dbconnect exists with file permissions that enable PHP to read the file. The file contains the PDO DSN to connect to a DB2 database through the PDO_ODBC driver: odbc:DSN=SAMPLE;UID=john;PWD=mypass |
The PHP script can then create a database connection by simply passing the uri: parameter and pointing to the file URI: <?php $dsn = 'uri:file:///usr/local/dbconnect'; $user = ''; $password = '';
try { $dbh = new PDO($dsn, $user, $password); } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); }
?>
|
|
例子 3. Create a PDO instance using an alias The following example assumes that php.ini contains the following entry to enable a connection to a MySQL database using only the alias mydb: [PDO]
pdo.dsn.mydb="mysql:dbname=testdb;host=localhost" |
<?php $dsn = 'mydb'; $user = ''; $password = '';
try { $dbh = new PDO($dsn, $user, $password); }catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); }
?>
|
|
其他的方法请参考手册:http://cn.php.net/pdo 说明:以上内容基本来源于手册,请自行参考手册获取更详细的内容。
|