分享
 
 
 

PHP5中数据库抽象层: PDO

王朝php·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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: extension=php_pdo.dll

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

PDO DriversThe following drivers currently implement the PDO interface:

Driver nameSupported databasesPDO_DBLIB

FreeTDS / Microsoft SQL Server / Sybase

PDO_FIREBIRD

Firebird/Interbase 6

PDO_MYSQL

MySQL 3.x/4.0

PDO_OCI

Oracle Call Interface

PDO_ODBC

ODBC v3 (IBM DB2 and unixODBC)

PDO_PGSQL

PostgreSQL

PDO_SQLITE

SQLite 3.x

预定义类PDORepresents a connection between PHP and a database server.

构造函数

PDO - constructs a new PDO object

方法

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

PDOStatementRepresents 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

// Connect to an ODBC database using driver invocation

$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

// Connect to an ODBC database using driver invocation

$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

// Connect to an ODBC database using an alias

$dsn = 'mydb';

$user = '';

$password = '';

try {

$dbh = new PDO($dsn, $user, $password);

}catch (PDOException $e) {

echo 'Connection failed: ' . $e->getMessage();

}

?>

其他的方法请参考手册:http://cn.php.net/pdo

说明:以上内容基本来源于手册,请自行参考手册获取更详细的内容。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有