|
相信写PHP程序的朋友都一直在用ADODB了,或者就是PearDB(简称PDB),这类抽象层数据库对象驱动(Abstraction Database Object Driver简称ADO)在数据库间迁移的通用性和开发程序的易用性上做出了很大的贡献,我们再也不用按部就班的写query....result.....fetchXXXXX了,像fetchAll,fetchAssoc那样的功能能让我们写更漂亮,更简洁的语句却能做更多的事情.大大减少代码冗余.提高了程序的通用性和可读性.调试起来也更方便轻松.
可是深入剖析,其实ADO也有一些小小的缺点和不足,高厚度的代码封装使得性能降低,速度自然比不上php自己"原装"的C API;而PHP(特别是4.X的版本)并不是一个完全的OOP语言(毕竟PHP是用C写的),在大数据面前,程序性能上还是比标准C API慢一些.当然开发者也想了不少方法去提升ADO的性能,比如ADODB的C API扩展,(http://phplens.com/lens/dl/adodb-ext-502.zip)
虽然能显著提升性能,但是那样又对程序的可移植性打了一个要命的折扣.这样各位同仁对PHP标准的ADO接口已经呼之欲出了.pecl终于在去年中推出了PDO( --The PHP Data Objects (PDO) extension )
http://cn.php.net/pdo
http://pecl.php.net/package/PDO
目前PDO版本为0.3,还在beta之列,支持数据库如下:
Driver name Supported databases
PDO_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
基本上99%的php程序都是用以上几个了吧,其中sqlite还是3.0的API,完全支持unicode,尚不支持Mysql4.1倒是一个小遗憾,不过日子还长呢
安装方法很简单:下载pdo核心扩展和你需要的数据库支持,笔者只下了mysql,pgsql和sqlite的,基本上这么多就足够了.
http://pecl.php.net/package/PDO
http://pecl.php.net/package/PDO_MYSQL
http://pecl.php.net/package/PDO_PGSQL
http://pecl.php.net/package/PDO_SQLITE
安装基本就是解压/进入目录/运行phpize/make; make install/修改php.ini
windows版本有现成的dll扩展:
http://snaps.php.net/win32/PECL_5_0/
或者从我这个帖子的附件里得到,只能工作在php5.0.4下,安装就是将几个dll文件复制到php的安装目录并修改php.ini,然后重启你的web服务器软件,就可以开始工作了..........
连接方式也比较简单:
[PHP]
$dsn = "{$_CONFIG['db_type']}:dbname={$_CONFIG['db_name']};host={$_CONFIG['db_host']}";
//connect
$DB =& new PDO($dsn, $_CONFIG['db_username'], $_CONFIG['db_password']);
//get more general settings in db
$res = $DB->query("select * from config where 1");
$row = $res->fetch();
[/PHP]
主要的方法(懒得翻译了):
PDO::beginTransaction -- Initiates a transaction
PDO::commit -- Commits a transaction
PDO::__construct -- Creates a PDO instance representing a connection to a database
PDO::errorCode -- Fetch the SQLSTATE associated with the last operation on the database handle
PDO::errorInfo -- Fetch extended error information associated with the last operation on the database handle
PDO::exec -- Execute an SQL statement and return the number of affected rows
PDO::getAttribute -- Retrieve a database connection attribute
PDO::lastInsertId -- Returns the ID of the last inserted row
PDO::prepare -- Prepares a statement for execution and returns a statement object
PDO::query -- Executes an SQL statement, returning a result set as a PDOStatement object
PDO::quote -- Quotes a string for use in a query.
PDO::rollBack -- Rolls back a transaction
PDO::setAttribute -- Set an attribute
PDOStatement::bindColumn -- Bind a column to a PHP variable
PDOStatement::bindParam -- Binds a parameter to a the specified variable name
PDOStatement::columnCount -- Returns the number of columns in the result set
PDOStatement::errorCode -- Fetch the SQLSTATE associated with the last operation on the statement handle
PDOStatement::errorInfo -- Fetch extended error information associated with the last operation on the statement handle
PDOStatement::execute -- Executes a prepared statement
PDOStatement::fetch -- Fetches the next row from a result set
PDOStatement::fetchAll -- Returns an array containing all of the result set rows
PDOStatement::fetchSingle -- Returns the first column in the next row of a result set
PDOStatement::getAttribute -- Retrieve a statement attribute
PDOStatement::getColumnMeta -- Returns metadata for a column in a result set
PDOStatement::rowCount -- Returns the number of rows affected by the last SQL statement
PDOStatement::setAttribute -- Set a statement attribute
PDOStatement::setFetchMode -- Set the default fetch mode for this statement
方法还比较少,不过毕竟还在开发中,相信以后回有更多的东西.
我在这几天将对adodb和pdo进行全面的测试,希望大家不要错过! |
|