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
说明:以上内容基本来源于手册,请自行参考手册获取更详细的内容。
分享到:
相关推荐
PHP数据库抽象层:是指封装了数据库底层的操作的,介于PHP逻辑程序代码和数据库之间的中间件。这就意味当从一个数据库系统向另一个数据库系统迁移时,几乎不用更改太多的程序代码,屏蔽不同数据库之间的差异 PDO:...
它提供了一个抽象层来访问数据,不管您使用的是哪个数据 库,并确保有一个单独的对象实例/数据库连接。它可以连接到几个可能的SQL数据库使用可用的PDO驱动程序。目前它支持MySQL、 PostgreSQL、Oracle、Microsoft SQL ...
下面就来介绍一下数据库抽象层PDO的使用: PDO(PHP Data Objects)是一个轻量级的PHP扩展,提供了一个数据访问抽象层。还要就是PDO只能在PHP5.0以上版本使用。 下面来介绍一下PDO常用的预定义常量: PDO::PARAM_BOOL...
本文实例讲述了php在数据库抽象层简单使用PDO的方法。分享给大家供大家参考,具体如下: 测试代码如下: <?php /************************** @Filename: pdotest.php @Content : PDO操作MySQL,Access(测试) ***...
PHP中PDO数据库抽象层的应用研究.pdf
PDO扩展为PHP访问数据库定义了一个轻量级的,一致性的接口,它提供了一个数据访问抽象层,这样,无论使用什么数据库,都可以通过一致的函数执行查询和获取数据. PDO支持的PHP版本为PHP5.1以及更高的版本,而且在PHP5.2下...
组件-数据库使用 PDO 的 PHP 5.3+ 数据库抽象层。 ###要求PDO FluentPDO (包含) Phpf\Util ##基本用法首先,初始化数据库设置; 连接本身是延迟加载的。 use Phpf \ Database \ Database ;Database :: init ('...
目前而言,实现“数据库抽象层”任重而道远,使用PDO 这样的“数据库访问抽象层” 是一个不错的选择。 PDO->beginTransaction() — 标明回滚起始点 PDO->commit() — 标明回滚结束点,并执行SQL PDO->__construct() ...
完整版 MySQL8.0从入门到精通 MySQL数据库教程 第23章 PDO数据库抽象类库(共12页).ppt 完整版 MySQL8.0从入门到精通 MySQL数据库教程 第24章 开发网上商城(共6页).ppt 完整版 MySQL8.0从入门到精通 MySQL数据库...
它所提供的数据接入抽象层,具有与具体数据库类型无关的优势,为它所支持的数据库提供统一的操作接口。目前支持的数据库有Cubrid、FreeTDS / Microsoft SQL Server / Sybase、Firebird/Interbase 6、IBM DB2、IBM ...
pdo_数据抽象层.chm内容包括五个常见 PHP 数据库问题和数据对象 (PDO) 抽象层与 Oracle————经验的总结。
PDO:PHP Data Object,PHP数据对象(数据抽象层) 作用:能够解决用户在需要使用不同的数据库的时候进行来回的切换,PDO能够自动的进行数据库的切换,而且还能将所有的有可能存在的不兼容的语法进行兼容性处理。
强大的 PHP 数据库抽象层 (DBAL),具有许多用于数据库模式自省和管理的功能。 Doctrine 数据库抽象和访问层 (DBAL) 围绕类似 PDO 的 API 提供了一个轻量级和精简的运行时层,以及许多附加的横向功能,例如通过 OO ...
主要:PDO扩展只是一个抽象的接口层,利用PDO扩展本身并不能实现任何数据库操作,必须使用一个特定的数据库PDO驱动访问数据库 2、启动PDO方法:找到php.ini文件将 复制代码 代码如下: ;extension=php_pdo.dll 前...
教义DBAL 强大的数据库抽象层,具有许多用于数据库模式自省,模式管理和PDO抽象的功能。 更多资源:
它提供了一个数据访问抽象层,不管php连接的是什么数据库都可以通过一致的函数执行查询和获取数据。PDO在php5.1发行时开始附带,php之前的版本不支持用于支付的抽象的PHP库特性:(1)灵活性,可以在php运行期间,...
文档介绍了PHP如何通过pdo操作MySQL数据库,说明很详细,还有很多具体的代码示例!
PDO(PHP Database Object)扩展为PHP访问数据库定义了一个轻量级的、一致性的接口,它提供了一个数据访问抽象层,这样,无论使用什么数据库,都可以通过一致的函数执行查询和获取数据。在数据库操作方面更加安全...