<?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(); ?>
* 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的数据
http://www.liuzhongwei.com/PHPjc/319757.htmlwww.liuzhongwei.comtruehttp://www.liuzhongwei.com/PHPjc/319757.htmlTechArticle1. 简介这是一部指导我们如何使用Pear DB扩展。Pear DB,提供这样一系列的类: n 数据库抽象 n 高级错误处理机制 n 以及其它 2. 下载、安装…