Home > Backend Development > PHP Tutorial > Pear DB 新手入门指南教程_PHP

Pear DB 新手入门指南教程_PHP

WBOY
Release: 2016-06-01 12:24:16
Original
958 people have browsed it

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示例:

<font face="黑体">存放</font>Pear<font face="黑体">的目录:</font>
Copy after login
<b># cd /usr/local/lib</b>
Copy after login
<font face="黑体">用“</font>phpfi<font face="黑体">“口令登录</font>:
Copy after login
<b># cvs -d :pserver:cvsread@cvs.php.net:/repository login</b>
Copy after login
<font face="黑体">用以下命令得到所有的</font>pear<font face="黑体">文件,同时也可以用来更新已经下载的文件。其他的参数有:</font>"today", "last month",<font face="黑体">等。我推荐用</font>"last week"<font face="黑体">参数,因为一般</font>bugs<font face="黑体">的提交和修改都是每周一次。</font> 
Copy after login
<b># cvs -d :pserver:cvsread@cvs.php.net:/repository export -D "last week" php4/pear</b>
Copy after login
<font face="黑体">编辑</font>php.ini<font face="黑体">文件加上下面一段在</font>include_path<font face="黑体">处:</font><i> /usr/local/lib/php4/pear</i> <font face="黑体">如果没有修改的权限,可以通过这条语句在代码中实现:</font> <i>ini_set('include_path', 'path_to_pear');</i>
Copy after login

获得PHP CVS的完全文档

注意Pear DB必需PHP版本4.0.4以上,而在Pear中的一些其他包如:XML Parser of the pear installer script需要PHP4.0.5以上版本。

 

3.        使用Pear DB

3.1         连接,断开数据库

<code> </code>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
<code><?php </CODE><br> <code>// The pear base directory must be in your include_path</code><br> <code>require_once </code><code>'DB.php'</code><code>;</code><br> <code>$user </code><code>= </code><code>'foo'</code><code>;</code><br> <code>$pass </code><code>= </code><code>'bar'</code><code>;</code><br> <code>$host </code><code>= </code><code>'localhost'</code><code>;</code><br> <code>$db_name </code><code>= </code><code>'clients_db'</code><code>;</code><br><br> <code>// Data Source Name: This is the universal connection string</code><br> <code>$dsn </code><code>= </code><code>"mysql://$user:$pass@$host/$db_name"</code><code>;</code><br><br> <code>// DB::connect will return a Pear DB object on success</code><br> <code>// or a Pear DB Error object on error</code><br> <code>// You can also set to TRUE the second param</code><br> <code>// if you want a persistent connection:</code><br> <code>// $db = DB::connect($dsn, true);</code><br> <code>$db </code><code>= </code><code>DB</code><code>::</code><code>connect</code><code>(</code><code>$dsn</code><code>);</code><br><br> <code>// With DB::isError you can differentiate between an error or</code><br> <code>// a valid connection.</code><br> <code>if (</code><code>DB</code><code>::</code><code>isError</code><code>(</code><code>$db</code><code>)) {</code><br> <code>        die (</code><code>$db</code><code>-></code><code>getMessage</code><code>());</code><br> <code>}</code><br> <code>....</code><br> <code>// You can disconnect from the database with:</code><br> <code>$db</code><code>-></code><code>disconnect</code><code>();</code><br> <code>?></code><code></code></code>
Copy after login
<code> </code>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

数据源(上例中的$dsn 参数)有以下允许的格式:(从Pear/DB.phpparseDSN方法复制而来)

<code> </code>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
<code>     *  phptype: Database backend used in PHP (mysql, odbc etc.)</code><br> <code>     *  dbsyntax: Database used with regards to SQL syntax etc.</code><br> <code>     *  protocol: Communication protocol to use (tcp, unix etc.)</code><br> <code>     *  hostspec: Host specification (hostname[:port])</code><br> <code>     *  database: Database to use on the DBMS server</code><br> <code>     *  username: User name for login</code><br> <code>     *  password: Password for login</code><br> <code>     *</code><br> <code>     * The format of the supplied DSN is in its fullest form:</code><br> <code>     *</code><br> <code>     *  phptype(dbsyntax)://username:password@protocol+hostspec/database</code><br> <code>     *</code><br> <code>     * Most variations are allowed:</code><br> <code>     *</code><br> <code>     *  phptype://username:password@protocol+hostspec:110//usr/db_file.db</code><br> <code>     *  phptype://username:password@hostspec/database_name</code><br> <code>     *  phptype://username:password@hostspec</code><br> <code>     *  phptype://username@hostspec</code><br> <code>     *  phptype://hostspec/database</code><br> <code>     *  phptype://hostspec</code><br> <code>     *  phptype(dbsyntax)</code><br> <code>     *  phptype</code><code></code>
Copy after login

现在支持的数据库有 ( phptype DSN 部分):

<code> </code>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
<code>mysql  -> MySQL</code><br> <code>pgsql  -> PostgreSQL</code><br> <code>ibase  -> InterBase</code><br> <code>msql   -> Mini SQL</code><br> <code>mssql  -> Microsoft SQL Server</code><br> <code>oci8   -> Oracle 7/8/8i</code><br> <code>odbc   -> ODBC (Open Database Connectivity)</code><br> <code>sybase -> SyBase</code><br> <code>ifx    -> Informix</code><br> <code>fbsql  -> FrontBase</code><code></code>
Copy after login

注意并不是所有数据库特征都支持,可以从根目录>/DB/STATUS 得到详细的清单。

3.2         执行数据库

<code> </code>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
<code><?php </CODE><br> <code>// Once you have a valid DB object</code><br> <code>...</code><br> <code>$sql </code><code>= </code><code>"select * from clients"</code><code>;</code><br> <code>// If the query is a "SELECT", $db->query will return</code><br> <code>// a DB Result object on success.</code><br> <code>// Else it simply will return a DB_OK</code><br> <code>// On failure it will return a DB Error object.</code><br> <code>$result </code><code>= </code><code>$db</code><code>-></code><code>query</code><code>(</code><code>$sql</code><code>);</code><br> <code>// Always check that $result is not an error</code><br> <code>if (</code><code>DB</code><code>::</code><code>isError</code><code>(</code><code>$result</code><code>)) {</code><br> <code>        die (</code><code>$result</code><code>-></code><code>getMessage</code><code>());</code><br> <code>}</code><br> <code>....</code><br> <code>?></code><code></code></code>
Copy after login
<code> </code>
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

 

3.3         获得select的数据

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template