1. 简介这是一部指导我们如何使用pear db扩展。pear db,提供这样一系列的类:
n 数据库抽象
n 高级错误处理机制
n 以及其它
2. 下载、安装pear 由于现在pear项目仍处于紧锣密鼓的开发之中,所以得到它的最好办法就是从cvs获得(pear db发行包已经跟随php4.0.6以后版本捆绑发布)。所以,我们只需要把pear的根目录放到php.ini配置文件include_path中。也可以通过这样设置:
_set('include_path', '/pear_base_dir').
以下是strp by step示例:
存放pear的目录: # cd /usr/local/lib 用“phpfi“口令登录: # cvs -d :pserver:cvsread@cvs.php.net:/repository login 用以下命令得到所有的pear文件,同时也可以用来更新已经下载的文件。其他的参数有:"today", "last month",等。我推荐用"last week"参数,因为一般bugs的提交和修改都是每周一次。 # cvs -d :pserver:cvsread@cvs.php.net:/repository export -d "last week" php4/pear 编辑php.ini文件加上下面一段在include_path处: /usr/local/lib/php4/pear 如果没有修改的权限,可以通过这条语句在代码中实现: ini_set('include_path', 'path_to_pear'); |
注意pear db必需php版本4.0.4以上,而在pear中的一些其他包如:xml parser of the pear installer script需要php4.0.5以上版本。
3. 使用pear db
3.1 连接,断开数据库
<?php // the pear base directory must be in your include_path require_once 'db.php'; $user = 'foo'; $pass = 'bar'; $host = 'localhost'; $db_name = 'clients_db';
// data source name: this is the universal connection string $dsn = "mysql://$user:$pass@$host/$db_name";
// db::connect will return a pear db object on success // or a pear db error object on error // you can also set to true the second param // if you want a persistent connection: // $db = db::connect($dsn, true); $db = db::connect($dsn);
// with db::iserror you can differentiate between an error or // a valid connection. if (db::iserror($db)) { die ($db->getmessage()); } .... // you can disconnect from the database with: $db->disconnect(); ?> |
数据源(上例中的$dsn 参数)有以下允许的格式:(从pear/db.php的parsedsn方法复制而来)
* phptype: database backend used in php (mysql, odbc etc.) * dbsyntax: database used with regards to sql syntax etc. * protocol: communication protocol to use (tcp, unix etc.) * hostspec: host specification (hostname[:port]) * database: database to use on the dbms server * username: user name for login * password: password for login * * the format of the supplied dsn is in its fullest form: * * phptype(dbsyntax)://username:password@protocol+hostspec/database * * most variations are allowed: * * phptype://username:password@protocol+hostspec:110//usr/db_file.db * phptype://username:password@hostspec/database_name * phptype://username:password@hostspec * phptype://username@hostspec * phptype://hostspec/database * phptype://hostspec * phptype(dbsyntax) * phptype |
现在支持的数据库有 (在 phptype dsn 部分):
mysql -> mysql pgsql -> postgresql ibase -> interbase msql -> mini sql mssql -> microsoft sql server oci8 -> oracle 7/8/8i odbc -> odbc (open database connectivity) sybase -> sybase ifx -> informix fbsql -> frontbase |
注意并不是所有数据库特征都支持,可以从根目录>/db/status 得到详细的清单。
3.2 执行数据库
<?php // once you have a valid db object ... $sql = "select * from clients"; // if the query is a "select", $db->query will return // a db result object on success. // else it simply will return a db_ok // on failure it will return a db error object. $result = $db->query($sql); // always check that $result is not an error if (db::iserror($result)) { die ($result->getmessage()); } .... ?> |
3.3 获得select的数据
1
如对本文有疑问,
点击进行留言回复!!
相关文章:
-
-
-
-
-
-
-
-
-
管理后台无限级菜单
管理后台菜单遍历很久就想做一个通用的管理后台出来,一直太懒没做,今天开始粗略搞了一下,只是先把框架搭建好,菜单栏...
[阅读全文]
-
桥接模式学习总结
桥接模式引言假如一个系统需要加入报表模块,主要的开发点是数据的获取和报表的显示方式。数据的获取可能是从系统数据库...
[阅读全文]
-
网友评论