﻿<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Xixi&#039;s Blog &#187; pear db</title>
	<atom:link href="http://www.xixis.net/archives/tag/pear-db/feed" rel="self" type="application/rss+xml" />
	<link>http://www.xixis.net</link>
	<description>http://www.xixis.net  &#124;  Eternal Sunshine of the Spotless Mind</description>
	<lastBuildDate>Thu, 12 Jan 2012 06:04:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>pear mdb 数据抽象层</title>
		<link>http://www.xixis.net/archives/pear-mdb-data-abstraction-layer.html</link>
		<comments>http://www.xixis.net/archives/pear-mdb-data-abstraction-layer.html#comments</comments>
		<pubDate>Wed, 10 Feb 2010 17:50:34 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[mdb]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[pear db]]></category>
		<category><![CDATA[数据中间层]]></category>
		<category><![CDATA[数据库]]></category>

		<guid isPermaLink="false">http://www.xixis.net/develop/php/pear-mdb-%e6%95%b0%e6%8d%ae%e6%8a%bd%e8%b1%a1%e5%b1%82/</guid>
		<description><![CDATA[Write once &#8211; run anywhere 一次编写——随处运行  这是Java的一句行销口号，但是它同时也是 PHP的关键特性之一。许多商业模型依赖于操作系统无关性来保证产品能够销售给广泛的客户群体。因而，为什么要把你自己绑在某种数据库厂商的身上呢？数据库抽象层使得你能够与数据库独立的开发你的应用程序。但是，通常情况下它们对性能的影响超过了你所希望的，要么他们并不足够抽象以消除所有和特定数据库相关的代码。  这篇文章将教给我什么？  这篇文章将对数据库抽象包 PEAR MDB 有一个很好的介绍。文章的焦点将是对 MDB 超越类似包所提供的更先进的特性，例如数据类型抽象和基于 XML 的 schema 管理。对 PHP 和 SQL 的基本理解是推荐的。  为什么另外再要一个数据库类？ 通常， web 工程在客户已经确定了要使用那种 RDBMS （关系型数据库管理系统）之后被添加给已经存在的 IT 基础结构。即使那并不是因为不同的预算可能影响的你选择何种数据用于部署的情况。最终，你作为开发者可能简单的偏好于不把自己绑在某个厂商身上。自此，意味着给每个支持的数据保持版本或者牺牲更多性能但是获得多于必须的易用性：走入 PEAR MDB 吧。  MDB 是着眼于使得编写 RDBMS 无关的 PHP 程序成为简单的过程的数据库抽象层。大部分其他的 PHP 的所谓数据库抽象层紧紧给所有支持的数据库提供了一个公用 API 以及非常有限的抽象（大部分只是针对序列的）。MDB 另一方面能够用来抽象所有数据库发送和接收的数据。甚至数据库 schema 都能被定义为 RDBMS 无关的格式。但是它提供这些功能的同时仍然保持了很高的性能以及简单易用。这是通过深入观察两个流行的数据库抽象层，PEAR DB 和 Metabase， 之后并且对它们进行了融合后获得的。而且在融合过程中，趁着这个机会清理了它们融合后的 API 以及任何影响性能的设计。  MDB 是怎样出现的？  早在 2001 年的秋天，我就在寻找一种可能能够让我公司的程序框架与 RDBMS 独立的数据库抽象包。这个目标是把特定数据库相关的代码数量减少到零。我发现提供这样的功能的唯一的一个包是 Metabase。但是 Metabase有一些部分是因为为了和 PHP3 兼容的让人不舒服的 API。尽管如此，我们决定 Metabase 是我们唯一的选择。但是即使是在给 Metabase 增加了一个性能改进的补丁之后，我们仍然感到我们放弃了太多的性能。我们在 2001 年的 PHP 国际会议上碰到了 Metabase 的作者，并且我们谈论了让像 Metabase 这样的东西成为 PEAR 工程一部分的好处。后来不久，在 PEAR 邮件列表上就 ...]]></description>
			<content:encoded><![CDATA[<p>Write once &#8211; run anywhere<br /> 一次编写——随处运行<br />  这是Java的一句行销口号，但是它同时也是 PHP的关键特性之一。许多商业模型依赖于操作系统无关性来保证产品能够销售给广泛的客户群体。因而，为什么要把你自己绑在某种<span class='wp_keywordlink_affiliate'><a href="http://www.xixis.net/archives/tag/%e6%95%b0%e6%8d%ae%e5%ba%93" title="显示数据库的所有日志" target="_blank">数据库</a></span>厂商的身上呢？<span class='wp_keywordlink_affiliate'><a href="http://www.xixis.net/archives/tag/%e6%95%b0%e6%8d%ae%e5%ba%93" title="显示数据库的所有日志" target="_blank">数据库</a></span>抽象层使得你能够与数据库独立的开发你的应用程序。但是，通常情况下它们对性能的影响超过了你所希望的，要么他们并不足够抽象以消除所有和特定数据库相关的代码。<br />  这篇文章将教给我什么？<br />  这篇文章将对数据库抽象包 PEAR MDB 有一个很好的介绍。文章的焦点将是对 MDB 超越类似包所提供的更先进的特性，例如数据类型抽象和基于 XML 的 schema 管理。对 PHP 和 SQL 的基本理解是推荐的。<br /> <span id="more-355"></span> <strong>为什么另外再要一个数据库类？</strong><br /> 通常， web 工程在客户已经确定了要使用那种 RDBMS （关系型数据库管理系统）之后被添加给已经存在的 IT 基础结构。即使那并不是因为不同的预算可能影响的你选择何种数据用于部署的情况。最终，你作为开发者可能简单的偏好于不把自己绑在某个厂商身上。自此，意味着给每个支持的数据保持版本或者牺牲更多性能但是获得多于必须的易用性：走入 PEAR MDB 吧。<br />  MDB 是着眼于使得编写 RDBMS 无关的 PHP 程序成为简单的过程的数据库抽象层。大部分其他的 PHP 的所谓数据库抽象层紧紧给所有支持的数据库提供了一个公用 API 以及非常有限的抽象（大部分只是针对序列的）。MDB 另一方面能够用来抽象所有数据库发送和接收的数据。甚至数据库 schema 都能被定义为 RDBMS 无关的格式。但是它提供这些功能的同时仍然保持了很高的性能以及简单易用。这是通过深入观察两个流行的数据库抽象层，PEAR DB 和 Metabase， 之后并且对它们进行了融合后获得的。而且在融合过程中，趁着这个机会清理了它们融合后的 API 以及任何影响性能的设计。<br />  <strong>MDB 是怎样出现的？</strong><br />  早在 2001 年的秋天，我就在寻找一种可能能够让我公司的程序框架与 RDBMS 独立的数据库抽象包。这个目标是把特定数据库相关的代码数量减少到零。我发现提供这样的功能的唯一的一个包是 Metabase。但是 Metabase有一些部分是因为为了和 PHP3 兼容的让人不舒服的 API。尽管如此，我们决定 Metabase 是我们唯一的选择。但是即使是在给 Metabase 增加了一个性能改进的补丁之后，我们仍然感到我们放弃了太多的性能。我们在 2001 年的 PHP 国际会议上碰到了 Metabase 的作者，并且我们谈论了让像 Metabase 这样的东西成为 PEAR 工程一部分的好处。后来不久，在 PEAR 邮件列表上就 PEAR DB 和 Metabase 融合的可能的好处又开始了一场讨论。在我们公司进行了许多讨论之后，我们决定承担这个任务。数个月的艰辛工作之后，我们现在有了 MDB 的第一个稳定的 release。<br />  <strong>MDB 给你提供了什么？</strong><br />  MDB 结合了 PEAR DB 和 Metabase 的大部分特性。实际上，PEAR DB 的特性中唯一不再存在的是作为结果集返回一个对象。我们放弃了这个特性是因为这个特性不常用而且对于性能的损失是非常明显的。许多开发上的时间用在了使得 API 尽可能的好用。最终，MDB 非常高地提供了这些功能至少和 PEAR DB 一样快而且比 Metabase 快很多。这些最重要地特性的列表： <br /> <br />
<blockquote> OO 风格的 API<br /> 预准备的查询模拟<br /> 给所有传递进来以及从数据库中取出的数据的完全的数据类型抽象（包括 LOB 支持）<br /> 事务支持<br /> 数据库/表/索引/序列创建/抛弃/改变<br /> RDBMS 无关的数据库 schema 管理<br /> 继承进 PEAR 框架（PEAR 安装程序，PEAR 错误处理等） </p></blockquote>
<p>  <strong>那么它如何使用呢？</strong><br />  MDB 提供了一些非常先进的抽象特性。记住这些特性只是供选择的是很重要的。但是在编写 RDBMS 无关的 PHP 程序时使用它们是非常重要的。一个展示使用 MDB 是多么简单的例子在文章的结尾的 &#8220;链接和文献&#8221; 部分。如前面所说，文章的焦点是介绍使得 MDB 与其他 PHP 数据库抽象层不同的那些特性。你可以在随本期文章一同包装的 CD 中找到所有这些例子脚本的代码。 <br />  但是，首先我们需要把 MDB 安装上去。使用 PEAR 安装程序这其实非常容易。我不能在这篇文章中完整的讲述 PEAR 安装程序但是我听说下一期将非常详细的讨论 PEAR 框架的里里外外。让安装程序运行于 Windows 的工作在进行当作但是支持仍然有一点古怪。对于 *nix 系统你需要 PHP 的 CGI 版本安装在了你的系统并且简单地运行下面地命令：<br /> <br />
<blockquote> lynx -source go-<span class='wp_keywordlink_affiliate'><a href="http://www.xixis.net/archives/tag/pear" title="显示pear的所有日志" target="_blank">pear</a></span>.org|<span class='wp_keywordlink_affiliate'><a href="http://www.xixis.net/archives/tag/php" title="显示php的所有日志" target="_blank">php</a></span> </p></blockquote>
<p>   在安装完成之后你只需要再输入一行命令那么就全部搞定了。<br /> <br />
<blockquote> <span class='wp_keywordlink_affiliate'><a href="http://www.xixis.net/archives/tag/pear" title="显示pear的所有日志" target="_blank">pear</a></span> install MDB </p></blockquote>
<p>   如果前面的过程对你来说不管用，总是有从 PEAR MDB 主页中直接获得包的选项。URL 列于文章的最后。<br />  <strong>利用数据类型抽象</strong><br />  因为大部分数据库倾向于有一些个性或者怪癖，对于MDB来说把这些不同之处给开发者隐藏起来非常重要。MDB 通过定义自己的内部数据类型来达到这点：text，boolean，integer，decimal，float，date，time，time stamp，large objects（文件）。所有传递给数据库和从数据库获取的数据都能转换成 MDB 的内部格式或者从数据库的内部格式转化回来。本节相关的例子脚本能够再 datatype 目录中找到。<br />让我们看看下面的查询：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$session</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'098f6bcd4621d373cade4e832627b4f6'</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// set time out to 30 minutes </span>
<span style="color: #000088;">$timeout</span> <span style="color: #339933;">=</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">30</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// SELECT query showing how the datatype conversion works</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'SELECT createtime, user_id FROM sessions'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">' WHERE session = '</span><span style="color: #339933;">.</span><span style="color: #000088;">$session</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">' AND lastaccess &lt; '</span><span style="color: #339933;">.</span><span style="color: #000088;">$timeout</span><span style="color: #339933;">;</span></pre></div></div>

<p>这个查询如果发送给数据库的话八成要失败。原因是存储在 $name 中的值需要转换为正确的字符串格式。这也许意味着 $name 的内容可能有特殊的转义字符或者被引号包围。PEAR DB 为此提供了方法 DB:.quote()。在 MDB 中这个方法叫 MDB::getTextValue()。不同之处是 MDB 给每种前面所列的数据类型都提供了这样的函数。因而我们也能够把 $timeout 转换为正确的格式。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// convert $timeout to the MDB timestamp format</span>
<span style="color: #000088;">$timeout</span> <span style="color: #339933;">=</span> MDB_date<span style="color: #339933;">::</span><span style="color: #004000;">unix2Mdbstamp</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$timeout</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// SELECT query showing how the datatype conversion works</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'SELECT createtime, user_id FROM sessions'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">' WHERE session = '</span><span style="color: #339933;">.</span><span style="color: #000088;">$<span class='wp_keywordlink_affiliate'><a href="http://www.xixis.net/archives/tag/mdb" title="显示mdb的所有日志" target="_blank">mdb</a></span></span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getTextValue</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$session</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">' AND lastaccess &lt; '</span><span style="color: #339933;">.</span><span style="color: #000088;">$<span class='wp_keywordlink_affiliate'><a href="http://www.xixis.net/archives/tag/mdb" title="显示mdb的所有日志" target="_blank">mdb</a></span></span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getTimestampValue</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$timeout</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
为了作个演示，让我们假定我仅仅想要获取第一行。MDB::queryRow() 获得第一行，它释放结果集并且返回其内容，因而它正是我们所要的。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>br<span style="color: #339933;">/&gt;</span><span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">queryRow</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>但是不同的 RDBMS 返回像日期这样的数据时用的格式是不同的。因此，如果我们然后要对一些数据进行计算，不管选择的 RDBMS 是什么，把数据以相同的格式返回是重要的。这个可以由 MDB 半自动地完成。你所有需要做的是告诉你的结果列将是什么样的类型，MDB将处理转换的工作。最简单的办法是把这样的信息传递给查询函数。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$types</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'timestamp'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'integer'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">queryRow</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #339933;">,</span> <span style="color: #000088;">$types</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>这告诉 MDB 结果集的第一列类型是 &#8216;timestamp&#8217; 以及第二列是&#8217;integer&#8217;。所有查询函数能够接受这样的元信息作为可选的参数。数据还能事后用 MDB::setResultTypes() 来设置。取决于数据获取于的数据库，它然后将被相应的转换返回的数据。MDB 内部的 timestamps 的数据格式是遵循 ISO 8601 标准的。其他像 PEAR::Date 这样的包能够处理这种格式。MDB 还在 MDB_Date 类中提供了一些数据格式转换函数，它们能够被可选的包含。</p>
<p>因为相当多的 RDBMS 以相同的方法返回整数数据，没有必要转换整数数据。因而，为了获得稍许的性能改进你能够这么做：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$types</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'timestamp'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">queryRow</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #339933;">,</span> <span style="color: #000088;">$types</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>这样只有结果集的第一列会被转换。当然，如果 MDB 用于返回整数不同的数据库，这可能成为一个问题。然而，稍许的性能改善可能并不值得冒这个风险。但是再一次的，它显示了这些特性的使用仅仅是供选择的。</p>
<p>Listing 1 展示了一个使用预准备的查询的例子。如果你必须运行大量查询而唯一的差别是数据传递给数据库，但是查询的结构还是一样的，这些能够相当的方便。高级的数据库能够在内存中储存解析好的查询来加速性能。</p>
<p>Listing 1</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$alldata</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'one'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'un'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'two'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'deux'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'three'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'trois'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'four'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'quatre'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$p_query</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepareQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'INSERT INTO numbers VALUES (?,?,?)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$param_types</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'integer'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'text'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'text'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$alldata</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$mdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p_query</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #339933;">,</span> <span style="color: #000088;">$param_types</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>在 $alldata 中储存的所有四个数组将用于 execute 语句。数据将自动被转换为正确的格式。因为这是一个插入语句，MDB::execute() 的第二个参数被设置为 NULL 因为我们将没有任何结果列需要我们设置数据类型。</p>
<p>在支持的数据类型中还有 LOB （大对象），它使得我们能够在数据库中储存文件。二进制文件储存在 BLOB （二进制大对象）中而且普通文本文件储存在 CLOB （字符大对象）中。在 MDB 中你仅仅能够使用预准备的 INSERT 和 UPDATE 查询储存 LOB。使用 MDBA::setParamBlob() 或者 MDB::setParamClob() 你能够设置预准备查询的 LOB 域的值。两个函数都预期传递一个 LOB 对象，而它能够使用 MDB::createLob() 创建。 </p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$binary_lob</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'Type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'inputfile'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'FileName'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'./myfile.gif'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$blob</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createLob</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$binary_lob</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$character_lob</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'Type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'data'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'Data'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'this would be a very long string container the CLOB data'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$clob</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createLob</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$character_lob</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>如你能看到的，MDB::createLob() 被传递一个关系数组。Type 键的值可能是以下中的一个：data, inputfile 或者 outputfile。前两个用于你想要把 LOB 写入数据库的时候。如果你有一个储存在变量中的 LOB，你应当在需要使用 inputfile 时从文件直接读取 LOB。最后，outpufile 应当在你想要从数据库中读取 LOB 时使用。取决于你是否使用数据或者 inputfile 你需要给 Filename 键或者 Data 键指定一个值，像上面的例子那样。现在，我们将把前面的 LOB 储存到数据库中去。 </p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$p_query</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepareQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'INSERT INTO files (id, b_data, c_data) VALUES (1, ?, ?)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$mdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setParamBlob</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p_query</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$blob</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'b_data'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setParamClob</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p_query</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$clob</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'c_data'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">executeQuery</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$p_query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>为了从数据库中获取上面的文件，我们需要首先从数据库中选择数据并且使用 MDB::createLob() 创建 LOB 对象。<br />这次我们将设置 &#8216;Type&#8217; 为 &#8216;outputfile&#8217;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$mdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SELECT b_data FROM files WHERE id = 1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$binary_lob</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'Type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'outputfile'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'Result'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$result</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'Row'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'Field'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'b_data'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'Binary'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'FileName'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'./myfile2.gif'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$blob</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createLob</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$binary_lob</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>现在我们能够使用 MDB::readLob() 从结果集中读取 LOB。传递长度 0 给 MDB::readLob() 意味着整个 LOB 被读取和储存在我们前面指定的文件中。一旦任务完成了，我们可以把资源释放了。你也可以设置任何大于零的长度并且使用一个 while 循环检查 MDB::endofLob() 来读取 LOB。</p>
<blockquote><p>
$mdb->readLob($blob, $data, 0);
</p></blockquote>
<p>注意你不要把这个获取函数和 bulk 获取函数像 MDB::fetchAll()搞混了，因为这将在大部分 PHP 数据库扩展中导致问题。在一些时候，MDB 可能能够使用 bulk 获取函数获得 LOB。</p>
<p>如我们在这节所见，MDB 特性本身的原生数据类型集自动映射于数据库中的原生数据类型。这保证了无论我们发送和从数据库接收什么样的数据，它都能与使用的 RDBMS 无关的使用相同的格式。如我在本节开篇已经提到的，这明显需要数据库使用的数据类型是 MDB 预期的。这种需要被用于确保映射所耗费的代价很小。下一节将教给我们 MDB 如何辅助在数据库中使用正确的数据类型。</p>
<p><strong>使用 XML schema 文件</strong></p>
<p>利用在上个段落中描述的特性，你能编写真正的数据库独立的程序。但是 MDB 尝试向前更加迈出一步：它允许你用 XML 定义你的 schema。一个管理器把这种 schema 转换为给每种 RDBMS 的必要的 SQL 语句。这意味着你能对所有支持的 RDBMS 使用相同的 schema。本节的例子能够在 xml_schema 目录中找到。</p>
<p>我们现在将从头编写一个 XML schema 文件。首先，我们必须定义一个 XML 文档。数据库定义是包含在一个 database 标签之中的。数据库的名字是使用 name 标签定义的。create 标签告诉管理器数据库是否需要在它不存在的时候被创建。如果你把你的 schema 文件分割成好几个文件你你首先提交给管理器的那个文件中把 create 设置为 1。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>xml version<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span> encoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;ISO-8859-1&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;database&gt;
&lt;name&gt;auth&lt; /name&gt;
&lt;create&gt;1&lt;/create&gt;
&lt; /database&gt;</pre></div></div>

<p>可能你已经从数据库名 auth 猜出了这个数据库的目的是用于储存简单的验证程序的用户数据。Listing 2 定义了在其中我们能储存用户数据的表。</p>
<p>Listing 2</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;table<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> users<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;declaration<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;field<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>user_id<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>integer<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;notnull<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;">&lt; /notnull<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;unsigned<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/unsigned<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;default<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/default<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/field<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;field<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>handle<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>text<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;length<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>20<span style="color: #009900;">&lt; /length<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;notnull<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/notnull<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;default<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/default<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/field<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;field<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>is_active<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>boolean<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;notnull<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;">&lt; /notnull<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;default<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>N<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/default<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;">&lt; /field<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/declaration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/table<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>如你能看到的，如使用 XML 时可以预期的，东西变得有一些冗长。不用担心：我们有一个基于浏览器的工具称为 MDB_frontend 使得这个过程更加简单。我将在这篇文章的后面谈论这个工程。可能这极其详细地表格描述的优点是非常明显。前面例子中的表格被称为 users 并且我们定义了 3 个域：类型为整数的 user_id，类型为文本的 handle 和类型为逻辑型的 is_active。记住如果你如前一节那样传递了必要的元数据 MDB 为你处理类型抽象。你还不需要 MDB 把这些类型映射为你的 RDBMS 中的什么。在每个域声明中还能使用的其他标签是可选的：</p>
<blockquote><p>
length，notnull，unsigned 和 default。
</p></blockquote>
<p>下一件我们现在需要做的事情是通过在 user_id 域放置恰当的索引确保 user_id 是唯一的。索引定义就在声明标签之内（Listing 3）。</p>
<p>Listing 3:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>table<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>name<span style="color: #339933;">&gt;</span> users<span style="color: #339933;">&lt;/</span>name<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>declaration<span style="color: #339933;">&gt;&lt;</span>br <span style="color: #339933;">/&gt;&lt;</span>index<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>unique<span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">&lt;/</span>unique<span style="color: #339933;">&gt;&lt;</span>br <span style="color: #339933;">/&gt;&lt;</span>name<span style="color: #339933;">&gt;</span>user_id_index<span style="color: #339933;">&lt;/</span>name<span style="color: #339933;">&gt;&lt;</span>br <span style="color: #339933;">/&gt;&lt;</span>field<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>name<span style="color: #339933;">&gt;</span>user_id<span style="color: #339933;">&lt;/</span>name<span style="color: #339933;">&gt;&lt;</span>br <span style="color: #339933;">/&gt;&lt;</span>sorting<span style="color: #339933;">&gt;</span>ascending<span style="color: #339933;">&lt;/</span>sorting<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span> <span style="color: #339933;">/</span>field<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>index<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>declaration<span style="color: #339933;">&gt;&lt;</span>br <span style="color: #339933;">/&gt;&lt;/</span>table<span style="color: #339933;">&gt;</span></pre></div></div>

<p>在 listing 3 中的定义在域 user_id 中创建一个唯一的上升排序的名为 user_id_index 的索引。当然，我们可以简单地添加另外一个域标签在索引定义中指定多于一个的域。我们现在仍然没有提到的是为我们产生唯一的用户 id 的序列。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>sequence<span style="color: #339933;">&gt;&lt;</span>br<span style="color: #339933;">/&gt;&lt;</span>name<span style="color: #339933;">&gt;</span>users_user_id<span style="color: #339933;">&lt;/</span>name<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>start<span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">&lt;</span> <span style="color: #339933;">/</span>start<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>on<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>table<span style="color: #339933;">&gt;</span>users<span style="color: #339933;">&lt;</span> <span style="color: #339933;">/</span>table<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>field<span style="color: #339933;">&gt;</span>user_id<span style="color: #339933;">&lt;/</span>field<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span> <span style="color: #339933;">/</span>on<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>sequence<span style="color: #339933;">&gt;</span></pre></div></div>

<p>上一个例子非常的绕弯。一行行看过来，我们看到首先打开一个 sequence 标签，跟着一个指定序列名字的 name 标签。这之后跟着一个定义序列初始值的 start 标签。现在，我们打开一个可选的 on标签。这儿我们需要设置一个表中的指定域。这个信息是管理器用来把序列的值设置为 users 表的 user_id 域的最大值。如果 users 表是空的，作为替代使用的是 start 标签中指定的值。请注意在 start 标签中指定的值是我们调用 MDB::nextId() 返回的第一个值。</p>
<p>当然，你也能使用任何值初始化表。例如你可能想要用你总是想要包含在你的程序中的管理用户来初始化前面的表格。为了这么做，我们需要把一个 initialization 标签添加给 table 标签。Listing 4 定义了一在另外一用 insert 标签包括的行之后的行。</p>
<p>Listing 4</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>table<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>name<span style="color: #339933;">&gt;</span> users<span style="color: #339933;">&lt;/</span>name<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>initialization<span style="color: #339933;">&gt;&lt;</span>br <span style="color: #339933;">/&gt;&lt;</span>insert<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>field<span style="color: #339933;">&gt;&lt;</span>br <span style="color: #339933;">/&gt;&lt;</span>name<span style="color: #339933;">&gt;</span>user_id<span style="color: #339933;">&lt;/</span>name<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>value<span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">&lt;</span> <span style="color: #339933;">/</span>value<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>field<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>field<span style="color: #339933;">&gt;&lt;</span>br <span style="color: #339933;">/&gt;&lt;</span>name<span style="color: #339933;">&gt;</span>handle<span style="color: #339933;">&lt;/</span>name<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>value<span style="color: #339933;">&gt;</span>default<span style="color: #339933;">&lt;</span> <span style="color: #339933;">/</span>value<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>field<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>field<span style="color: #339933;">&gt;&lt;</span>br <span style="color: #339933;">/&gt;&lt;</span>name<span style="color: #339933;">&gt;</span>is_active<span style="color: #339933;">&lt;/</span>name<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>value<span style="color: #339933;">&gt;</span>Y<span style="color: #339933;">&lt;</span> <span style="color: #339933;">/</span>value<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>field<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>insert<span style="color: #339933;">&gt;&lt;</span>br <span style="color: #339933;">/&gt;&lt;/</span>initialization<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>table<span style="color: #339933;">&gt;</span></pre></div></div>

<p>如你从上个例子中能看到的那样，所有我们需要做的就是给表的每个域设定值。我们现在已经知道了必要的基础知识来创建一个 MDB 的 XML schema。下一步是把这个 schema 文件传递给 MDB 管理器。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$manager</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MDB_Manager<span style="color: #339933;">;</span>
<span style="color: #000088;">$input_file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'auth.schema'</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// we do not have to connect to a specify a specific database at this time</span>
<span style="color: #000088;">$dsn</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;mysql://<span style="color: #006699; font-weight: bold;">$user</span>:<span style="color: #006699; font-weight: bold;">$pass</span>@<span style="color: #006699; font-weight: bold;">$host</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$manager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dsn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$manager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">updateDatabase</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input_file</span><span style="color: #339933;">,</span> <span style="color: #000088;">$input_file</span><span style="color: #339933;">.</span> <span style="color: #0000ff;">'.before'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>我们现在有了一个新的名字叫 auth 的数据库，它有一个表叫 users。在域 user_id 有一个索引。而且在表中还有一行。我们还有一个序列称为 users_user_id，它将被初始化为 1。因此序列中的下一个值就是 2。最后，schema 的一个拷贝以名字auth.schema.before 被创建。这是因为我们给 MDB_Manger::updateDatabase() 传递了可选的第二个参数。在下一节我们将看到为什么要创建这个拷贝。</p>
<p>所有这些都非常令人惊奇但是它变得更好。许多情况下程序需要在某些地方作出改变。例如我们可能决定需要把表的名字从 users 变成 people。我们可能还需要增加一个域 pwd 来储存密码域（请检查 textbox 的保留字）。</p>
<p><strong>保留字</strong></p>
<p>我们没有称那个域为 password 的原因是那是 Interbase 中一个域名的保留字。因为我们需要 RDBMS 独立，MDB 管理器要么给出一个警告要么在 fail_on_invalid_names 选项被设置为真的时候（这是缺省值）失败。</p>
<p>在过去的时候，你可能现在正处于把你所有已经有的东西变成这种新的 schema 的痛苦之中。但是由于 MDB 这些工作能够自动完成。在 listing 5 中是我们对我们的表格定义进行的修改：</p>
<p>Listing 5</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;table<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> people<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;was<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>users<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/was<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;declaration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;field<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>pwd<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>text<span style="color: #009900;">&lt; /type<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;length<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>32<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/length<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;notnull<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/notnull<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;default<span style="color: #000000; font-weight: bold;">&gt;</span></span>&lt; /default<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/field<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;">&lt; /declaration<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/table<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>现在我们想要管理器来作出必要的改变，但是在此之前我像提一下可能的陷阱。因为我们把表从 users 更名为 people，我们还需要把所有对原来名字的引用进行更改，比如我们建立的序列。在 on 标签中的索引需要更改为指向 people 表。为了达到这个目的，我们把 shcema 的新旧版本传递给管理器。这酒是为什么我们在第一次调用 MDB_Manager::updateDatabase() 时我们创建一个 .before 文件的原因。这确保了我们有一个旧版本的 shcema 来与新的版本进行比照。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$input_file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'auth.schema'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$manager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">updateDatabase</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input_file</span><span style="color: #339933;">,</span> <span style="color: #000088;">$input_file</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.before'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>所有的就是这样！users 表现在称为 people 并且我们也有了一个 pwd 域。</p>
<p>我现在要看看 XML schema 格式的最后一个特性。如果你想要编程性的使用管理器，这个特性尤其重要。假设你有好几个有相同验证程序运行在你的数据库服务器的客户。每个客户有一个服务器运行在这个服务器有相同的 schema 只有微小的区别：数据库的名字。可能为每个客户单独保存 schema 文件是可行的因为更新周期可能不是一样的，这不是我们例子验证程序的情况。这儿所有的客户同时更新。XML schema 文件允许我们为此可以使用变量。</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;ISO-8859-1&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;database<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;variable<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>name<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/variable<span style="color: #000000; font-weight: bold;">&gt;</span></span>&lt; /name<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/database<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>我们现在在运行时设置变量为任意我们需要的东西。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"> <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$clients</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$variables</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span>
<span style="color: #000088;">$manager</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">updateDatabase</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input_file</span><span style="color: #339933;">,</span> <span style="color: #000088;">$input_file</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.before'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$variables</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>XML schema 管理是 MDB 提供的数据库抽象概念的另外一个非常重要的部分。它使得我们保持我们的 schema 定义与特定的 RDBMS 无关。但是使用这个格式还确保了使用正确的原生数据类型因而 MDB 能够正确地映射它的原生数据类型。最后，因为数据是基于 XML 的，编写产生或者读取 XML schema 文件的工具要容易一些。</p>
<p>听起来不错但是我的应用程序已经使用了……<br />
大部分读者可能发现它们处于这样的境地——他们已经有了大量运行于其他数据库抽象层的程序。由于 MDB 的出身，大部分 PEAR DB 的用户应当发现 MDB 感觉上非常类似，因为 MDB 的 API 是基于 PEAR DB 的。Metabase 用户应当发现他们所有偏爱的功能都在 MDB 中有对应的东西。XML schema 格式和 Metabase 中的是一摸一样的。一个完全的指导来引导你把已经写好的程序移植到 MDB 中超出了本文的范围，但是我将利用这个机会给一些提示。如果你有任何具体的问题，放心的发信来询问我。</p>
<p>为了把你的 PEAR DB 程序移植到 MDB，最好的起点是 PEAR wrapper。你能使用 PEAR wrapper 来运行你的程序。wrapper 当然增加了一些额外负担，因而你可能有些想要移植到原生的接口。那么第一步是列出所有你程序当前使用的 PEAR DB 函数。然后看看 wrapper 从中找出任何 API 上的区别。有两个你要注意的关键区别：结果集不再是对象而且所有的允许你传递结果集的数据类型的查询方法将导致参数顺序上的少许改变。第一个区别意味着不能再结果对象上调用获取函数。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchRow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>你现在必须调用 MDB 对象来进行获取：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$mdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchRow</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>第二个区别通过观察 wrapper 可以轻易的被解决。如你再 wrapper 中能看到的，你可以再 MDB 期望得到结果集的数据类型的地方简单地传递 NULL。现在，你地程序应当能够使用 MDB。当然，你现在没有真正得到了 MDB 地高级特性优点的益处。这最有可能的是需要对你当前的数据库 schema 进行一些改动。管理器能够尝试反向地从已经存在的数据库中获取 XML schema 文件。一个非常简单的前端可以在 MDB 包中找到：reverse_engineer_xml_schema.<span class='wp_keywordlink_affiliate'><a href="http://www.xixis.net/archives/tag/php" title="显示php的所有日志" target="_blank">php</a></span> 脚本。极有可能你将需要手动修正产生的 XML schema 恩见，但是它将给你一个很好的开始。</p>
<p>如果你想要把你已经存在的程序从 Metabase 移植到 MDB 你将必须改动所有的函数调用。查看 Metabase wrapper 需要改动什么将变得非常明显。如果你知道正则表达式你可能能够完成大部分这样的替换工作。无论如何，你应当向前并且运行你原来喜爱的高级抽象特性但是现在用的是 MDB。你可能注意得到的是函数名变得更加简短了。如果你作一些性能测试，你也将看到可观的性能改善。<br />
<br />
那么 MDB 将来会是什么样子呢？</p>
<p>本文发表时 MDB 可能已经不再是原来的 1.0 release 了。在原来的 MySQL 和 PostGreSQL 驱动之后，MDB还将有一个 ODBC 驱动以及可能的更多的驱动。这是 MDB 开发过程中关注的关键区域之一。一旦 MDB 在驱动方面跟上了 PEAR DB，它很有可能成为 PEAR 框架中标准的数据库抽象层。</p>
<p>但是还有另外一个开发中的关键领域：MDB_frontend 工程。MDB_frontend将成为基于 MDB 和 MDB 管理器的 phpMyadmin。有了这个工具，你将能够浏览储存在 MDB 支持的 RDBMS 中的数据库。<br />
MDB_frontend 将同时显示原生和 MDB 数据类型。模拟的特性比如 MySQL 中的序列将被隐藏。用户将仅仅看到一个序列列表而不是一个储存序列指的表，而在 MySQL 中这就是序列是如何被模拟的。而且 MDB_frontend 将帮助移植已经存在的数据库来符合 MDB 预期使用的原生数据类型。它还将帮助创建和更新 XML schema 文件。一些初期的工作已经完成了但是很多工作需要在公开发布之前被添加。</p>
<p>驱动和 MDB_frontend 是当前开发的所有焦点，在 MDB 中还有许多用户可能需要的：像 bulk 获取 LOB 域的集成，其他人可能需要外部和主键支持。如一直以来的那样如果你参与测试和实现，开源的东西将加快很多。但是我也很感谢像特性需求合阳的反馈。</p>
<p><strong>一些文后的思考</strong></p>
<p>在数月的艰辛工作之后，MDB 正在当前的 PEAR DB 和 Metabase 用户中获得认可。我还希望当前还没有被其他数据库抽象层说服的用户意识到 MDB 给他们的好处。当然，还是有许多程序需要对 RDBMS 进行特殊剪裁，对于这种情况像 MDB 这样的工具仅仅是增加了不必要的额外负担和限制。总的来说，我非常高兴我们在我们的公司中作出领导 MDB 开发的决定。在起初，我对尝试同时取悦 PEAR DB 和 Metabase 的用户但是结果可能到处不讨好多少有些担心。另外一个关心的来源是 PHP 社区是否将帮助其开发。我非常高兴 PHP 社区来了并且帮助撰写驱动以及 MDB 的核心。因而我们认为这个项目是一个极大的成功。我们还一并相信 MDB 将得到更大的改进。而且我们对帮助 PHP 变得更好感到高兴。</p>
<p>关于作者<br />
Lukas Smith 是 PEAR DB 的主要作者。它积极地给多个 PHP 开远项目进行贡献并且是专注于 PHP 开发的 BackendMeida 公司的建立者。</p>
<p>链接 和文献</p>
<p>PEAR MDB homepage: http://pear.php.net/package-info.php?package=MDB<br />
PEAR MDB documentation: http://www.backendmedia.com/MDB/docs/<br />
PEAR MDB sample script: http://cvs.php.net/co.php/pear/MDB/MDB_test.php<br />
PEAR DB homepage: http://pear.php.net/package-info.php?package=DB<br />
Metabase homepage: http://www.phpclasses.org /mirrors.html?page=％2Fbrowse.html％2Fpackage％2F20.html<br />
Simple benchmark: http://freshmeat.net/screenshots/30313/ </p>
]]></content:encoded>
			<wfw:commentRss>http://www.xixis.net/archives/pear-mdb-data-abstraction-layer.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PEAR：创建中间的数据库应用层</title>
		<link>http://www.xixis.net/archives/pear-create-a-middle-tier-database-applications.html</link>
		<comments>http://www.xixis.net/archives/pear-create-a-middle-tier-database-applications.html#comments</comments>
		<pubDate>Wed, 10 Feb 2010 17:27:05 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[pear db]]></category>
		<category><![CDATA[数据中间层]]></category>
		<category><![CDATA[数据库]]></category>

		<guid isPermaLink="false">http://www.xixis.net/develop/php/pear%ef%bc%9a%e5%88%9b%e5%bb%ba%e4%b8%ad%e9%97%b4%e7%9a%84%e6%95%b0%e6%8d%ae%e5%ba%93%e5%ba%94%e7%94%a8%e5%b1%82/</guid>
		<description><![CDATA[ 内容：  一、 什么是DB类  二、 为什么要设计抽象的中间数据层  三、 DB的使用入门  四、 DB_Common 使用参考  五、 更进一步，创建你自己的中间数据库应用层  六、 DB的不足  七、参考资源   关于作者     潘凡 (nightsailer@hotmail.com) 北京赛迪网信息技术有限公司 2001 年 8 月  对于PHP的应用程序来说，90％以上需要和数据库来打交道。那么，你是如何操纵数据库的？当你的后端数据库升级或变迁后，你的这些程序是否能够随之平滑地升级和挂接呢？如果你正在考虑这个问题，那么不妨和我来讨论一下，如何使用PEAR中的DB类来创建与数据库无关的数据库应用层。 
一、 什么是DB类 
 我们首先简单地了解一下DB类。DB类是PEAR中进行数据操作的几个类的集合，它的主要目的是提供一个统一的，抽象的数据接口，这个接口与后端的数据库是无关的。因此，如果你的应用程序使用这个通用的接口来进行数据库的操作，那么就能够平滑地切换到不同的数据库下面，如 MYSQL，SQL，SYBASE等等。实际上，DB类希望能够起到简单的类似ODBC或者是PERL中的DBI的作用。说到这里，不得不提一下PHP中的另一个优秀的库：ADODB。ADODB也和DB一样，提供了一个抽象的中间层，而且ADODB所支持的后端数据库要比DB多（至少目前如此），不过 ADODB没有直接使用PEAR的一些特性，只是吸取了PEAR的许多思想，包括DB，因此二者的使用方法有许多相似的地方。我不想评论二者孰优孰劣，大家可以根据个人的喜好来使用。 
二、 为什么要设计抽象的中间数据层 
 在详细讨论DB的使用之前，我们先讨论一下为什么要设计中间的数据层，因为这意味着你需要作出一些牺牲和让步，比如，你需要多写一些代码，有的局限于特定数据库的特性将无法直接使用。  我们回忆一下我们过去的做法，如何连接到MYSQL数据库？这的确是个小儿科的问题，下面的代码你一定很熟悉：

&#60;?php
/**
  * 连接到MYSQL数据库
  */
  $host   = &#34;localhost&#34;;
&#160;
  $user   = &#34;root&#34;;
  $passwd = &#34;&#34;;
  $persistent = 1;
&#160;
  if &#40;$persisternt&#41;&#123;
     $conn = mysql_connect&#40;$host,$user,$passwd&#41;;
  &#125;else &#123;
  ...]]></description>
			<content:encoded><![CDATA[<p> 内容： <br /> 一、 什么是DB类 <br /> 二、 为什么要设计抽象的中间数据层 <br /> 三、 DB的使用入门 <br /> 四、 DB_Common 使用参考 <br /> 五、 更进一步，创建你自己的中间<span class='wp_keywordlink_affiliate'><a href="http://www.xixis.net/archives/tag/%e6%95%b0%e6%8d%ae%e5%ba%93" title="显示数据库的所有日志" target="_blank">数据库</a></span>应用层 <br /> 六、 DB的不足 <br /> 七、参考资源 <br /> <span id="more-351"></span> 关于作者 <br />    潘凡 (nightsailer@hotmail.com)<br /> 北京赛迪网信息技术有限公司<br /> 2001 年 8 月<br />  对于PHP的应用程序来说，90％以上需要和<span class='wp_keywordlink_affiliate'><a href="http://www.xixis.net/archives/tag/%e6%95%b0%e6%8d%ae%e5%ba%93" title="显示数据库的所有日志" target="_blank">数据库</a></span>来打交道。那么，你是如何操纵<span class='wp_keywordlink_affiliate'><a href="http://www.xixis.net/archives/tag/%e6%95%b0%e6%8d%ae%e5%ba%93" title="显示数据库的所有日志" target="_blank">数据库</a></span>的？当你的后端数据库升级或变迁后，你的这些程序是否能够随之平滑地升级和挂接呢？如果你正在考虑这个问题，那么不妨和我来讨论一下，如何使用PEAR中的DB类来创建与数据库无关的数据库应用层。<br /> <br />
<h3>一、 什么是DB类 </h3>
<p> 我们首先简单地了解一下DB类。DB类是PEAR中进行数据操作的几个类的集合，它的主要目的是提供一个统一的，抽象的数据接口，这个接口与后端的数据库是无关的。因此，如果你的应用程序使用这个通用的接口来进行数据库的操作，那么就能够平滑地切换到不同的数据库下面，如 MYSQL，SQL，SYBASE等等。实际上，DB类希望能够起到简单的类似ODBC或者是PERL中的DBI的作用。说到这里，不得不提一下PHP中的另一个优秀的库：ADODB。ADODB也和DB一样，提供了一个抽象的中间层，而且ADODB所支持的后端数据库要比DB多（至少目前如此），不过 ADODB没有直接使用PEAR的一些特性，只是吸取了PEAR的许多思想，包括DB，因此二者的使用方法有许多相似的地方。我不想评论二者孰优孰劣，大家可以根据个人的喜好来使用。<br /> <br />
<h3>二、 为什么要设计抽象的中间数据层 </h3>
<p> 在详细讨论DB的使用之前，我们先讨论一下为什么要设计中间的数据层，因为这意味着你需要作出一些牺牲和让步，比如，你需要多写一些代码，有的局限于特定数据库的特性将无法直接使用。<br />  我们回忆一下我们过去的做法，如何连接到MYSQL数据库？这的确是个小儿科的问题，下面的代码你一定很熟悉：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?<span class='wp_keywordlink_affiliate'><a href="http://www.xixis.net/archives/tag/php" title="显示php的所有日志" target="_blank">php</a></span></span>
<span style="color: #009933; font-style: italic;">/**
  * 连接到MYSQL数据库
  */</span>
  <span style="color: #000088;">$host</span>   <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$user</span>   <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;root&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$passwd</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$persistent</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$persisternt</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     <span style="color: #000088;">$conn</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span><span style="color: #000088;">$user</span><span style="color: #339933;">,</span><span style="color: #000088;">$passwd</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #000088;">$conn</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_pconnect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span><span style="color: #000088;">$user</span><span style="color: #339933;">,</span><span style="color: #000088;">$passwd</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>好了，现在建立了数据库连接，我们可以使用它来进行数据库的操作，我们可能使用类似的代码：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?<span class='wp_keywordlink_affiliate'><a href="http://www.xixis.net/archives/tag/php" title="显示php的所有日志" target="_blank">php</a></span></span>
<span style="color: #000000; font-weight: bold;">function</span> sql_exec<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$db_Name</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_db_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db_dbName</span><span style="color: #339933;">,</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">echo</span> <span style="color: #990000;">mysql_errno</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;: &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br&gt;<span style="color: #006699; font-weight: bold;">$sql</span>&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>             
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$db_Name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;select * from users&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> sql_exec<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;姓名:<span style="color: #006699; font-weight: bold;">$row[0]</span> 性别：<span style="color: #006699; font-weight: bold;">$row[1]</span> 年龄 <span style="color: #006699; font-weight: bold;">$row[2]</span>&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #990000;">mysql_free_result</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>看起来很不错，是吗？你可能在你的代码里使用很多类似的代码片段。但是，不要太高兴，问题来了。假如，突然，你的数据库需要从MYSQL迁移到别的数据库平台，比如ORACLE，SYBASE。迁移的原因很多，也许是你的老板突发奇想，认为这样能卖个好价钱，或者是你的数据猛增，导致MYSQL的性能下降，总之，迁移是事在必行了。你怎么做，你也许会想，呵呵，这简单，把相关的函数替换一下不就行了。</p>
<p>听起来简单，但是……首先，连接数据库的函数要改，需要把 mysql_connect和mysql_pconnect替换成OCILogon和OCIPLogon。mysql_errno和 mysql_error()当然不能使用，你需要从OCIError()返回的数组中提取响应的信息。</p>
<p>这还不是太糟，最糟的是相关的mysql_fetch_row,mysql_fetch_array等语句遍布于你的许多代码函数和过程中，你需要逐一查找，分析，然后重新替换或者编写相应的ORACLE的版本。如果，你的数据库操作是集中在一个某一个模块或类中，这项工作还可以接受，否则，等于你重新阅读和修改了绝大部分的代码。即使这个不幸的人不是你，那么他也会暗地里诅咒你的；=）</p>
<p>以上，我们回忆了我们以前的做法，以及可能带来的不幸。那么，如果使用DB类来做类似的操作，应该是什么样的呢？下面是相应的DB版本代码： </p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?<span class='wp_keywordlink_affiliate'><a href="http://www.xixis.net/archives/tag/php" title="显示php的所有日志" target="_blank">php</a></span></span>
<span style="color: #b1b100;">include_once</span> <span style="color: #0000ff;">&quot;DB.php&quot;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/*
* 连接到数据库
*/</span>
   <span style="color: #000088;">$db_host</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$db_user</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;root&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$db_passwd</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$db_dbName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;test&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$PersistentConnection</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">;</span>
   <span style="color: #000088;">$db_type</span> <span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;mysql&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$db_proto</span> <span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> DB<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$db_type</span>://<span style="color: #006699; font-weight: bold;">$db_user</span>@<span style="color: #006699; font-weight: bold;">$db_passwd</span>:<span style="color: #006699; font-weight: bold;">$db_host</span>/<span style="color: #006699; font-weight: bold;">$db_dbName</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$db_options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> DB<span style="color: #339933;">::</span><span style="color: #004000;">isError</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
       <span style="color: #990000;">die</span> <span style="color: #0000ff;">&quot;无法连接数据库，错误原因：&quot;</span><span style="color: #339933;">.</span>DB<span style="color: #339933;">::</span><span style="color: #004000;">errorMessage</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> sql_exec<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$db</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>DB<span style="color: #339933;">::</span><span style="color: #004000;">isError</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;发生数据库错误：&quot;</span><span style="color: #339933;">.</span>DB<span style="color: #339933;">::</span><span style="color: #004000;">errorMessage</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>   
<span style="color: #666666; font-style: italic;">/////////////////////////////////////////////</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;select * from users&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> sql_exec<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchRow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;姓名:<span style="color: #006699; font-weight: bold;">$row[0]</span> 性别：<span style="color: #006699; font-weight: bold;">$row[1]</span> 年龄 <span style="color: #006699; font-weight: bold;">$row[2]</span>&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>除了连接数据库部分，其他的看起来只是有一些微小的变化，出错处理使用的是PEAR类似的方式（isError），实际上也是从PEAR继承来的。同样的情况，如果你要把数据库从mysql迁移到别的形式，这次假如说是 PostegreSQL,一个LINUX中很优秀的数据库，你所做的只是改变一行代码： </p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #000088;">$db_type</span> <span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;mysql&quot;</span><span style="color: #339933;">;</span>
变成：
   <span style="color: #000088;">$db_type</span> <span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;pgsql&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>其他的，不用变动。怎么样，升级的感觉是不是很清爽呢，你可以用剩下的时间好好研读其余的代码，或者和我继续往下讨论DB的使用方法。</p>
<h3>三、 DB的使用入门 </h3>
<p>
DB类由3部分组成：</p>
<blockquote><p>
DB.php 这是前端接口，在DB类里提供了许多&#8221;静态&#8221;的公用方法，我们一般只需要INCLUDE_ONCE这个文件就可以了。 <br />
DB/common.php 这是后端数据库的通用抽象类，不同的数据库的后端类需要继承并实现这个类中定义的公用方法和属性，如果你的数据库不被支持，你可以自己编写一个支持类，这样，你的应用程序就可以迁移过来了。 <br />
DB/storage.php 这是一个辅助的工具，它可以把SQL查询做为对象返回，同时能够维护这些对象，在对象改变的时候，相应地更新数据库。
</p></blockquote>
<p>
<blockquote>
DB/ifx.php <br />
   mssql.php  Ms SQL Server支持类<br />
   oci8.php   Orcale 8i支持类<br />
   pgsql.php  PostegreSQL支持类<br />
   sybase.php Sybase支持类<br />
   ibase.php  ibase支持类<br />
   msql.php   mSQL 支持类<br />
   mysql.php  mysql支持类<br />
   odbc.php   odbc 支持类
</p></blockquote>
<p>这些是相应后端数据库的支持类了。相应具体的数据库的操作是由这些支持类来实现的。<br />
下面，我们首先详细介绍DB.PHP中的一些&#8221;静态&#8221;方法： </p>
<h4>connect()方法</h4>
<p>
这个方法是最重要的静态方法了，我们通过得到一个DB_COMMON对象，并且连接到相应的数据库。这个方法的原型如下： </p>
<pre lang=php>function &#038;connect($dsn, $options = false)</pre>
<blockquote><p>
$dsn是数据源名称（data source name）的缩写,可以是字符串，或者是特定的数组形式。
</p></blockquote>
<p></p>
<p>一般来说，$dsn是一个字符串，它的格式如下： </p>
<blockquote><p>
phptype(dbsyntax)://username:password@protocol+hostspec/<span class='wp_keywordlink_affiliate'><a href="http://www.xixis.net/archives/tag/database" title="显示database的所有日志" target="_blank">database</a></span></p>
<p>     *  phptype:  php后端数据库的类型名称(如mysql, odbc 等等.)<br />
     *  dbsyntax: 数据库所使用的SQL语法标准，一般不用。<br />
     *  protocol: 使用的通讯协议。（如tcp, unix 等等.）<br />
     *  hostspec: 数据库所在的主机的描述。（形式是：主机名[:端口号]）<br />
     *  <span class='wp_keywordlink_affiliate'><a href="http://www.xixis.net/archives/tag/database" title="显示database的所有日志" target="_blank">database</a></span>: 数据库的名称。<br />
     *  username: 登陆的用户名。<br />
     *  password: 登陆的密码。
</p></blockquote>
<p>对于DSN，常用的形式如下： </p>
<blockquote><p>
     *  phptype://username:password@protocol+hostspec:110//usr/db_file.db<br />
     *  phptype://username:password@hostspec/database_name<br />
     *  phptype://username:password@hostspec<br />
     *  phptype://username@hostspec<br />
     *  phptype://hostspec/database<br />
     *  phptype://hostspec<br />
     *  phptype(dbsyntax)<br />
     *  phptype</p>
</blockquote>
<p>对于省略的部分，将使用缺省值。<br />
当然，$dsn也可以是一个数组，数组的形式如下： </p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$dsn</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
       <span style="color: #0000ff;">'phptype'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'mysql'</span><span style="color: #339933;">,</span>
       <span style="color: #0000ff;">'dbsyntax'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
       <span style="color: #0000ff;">'protocol'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span>
       <span style="color: #0000ff;">'hostspec'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span>
       <span style="color: #0000ff;">'database'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'test'</span><span style="color: #339933;">,</span>
       <span style="color: #0000ff;">'username'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'root'</span><span style="color: #339933;">,</span>
       <span style="color: #0000ff;">'password'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span>
       <span style="color: #009900;">&#41;</span></pre></div></div>

<p>$options 是数据库的选项，混合型。如果是布尔型，那么一般来说，这个参数指明是否使用持久性连接(persistent connect)，如果后端数据库支持，当$options是TRUE的时候，将使用持久性连接。如果是数组，那么表示这是特定的后端数据库的选项，这些选项将传递到DB_common类中的set_option方法中，后端数据库通过实现或重载这个方法，可以自己决定如何使用这些选项。 </p>
<h4>isError($value) </h4>
<p>
这个方法用来判断DB的一些方法返回的结果是否是一个错误对象，你可以使用这个方法来判定某个操作的结果是否是抛出了异常。</p>
<p>当然，如果你的应用程序从是PEAR继承的，也可以直接使用PEAR的isError来判断，尤其是当你的程序中抛出的异常的可能是数据库以外的异常的时候，这个方法只能判断是否是DB_Errro对象，其他的PEAR_Error对象是无法识别出的。</p>
<h4>function isWarning($value) </h4>
<p>
这个方法是判断DB方法的返回结果是否是一个警告。和错误不同的是，警告不是致命的，所以仍可以继续执行下去。</p>
<h4>function errorMessage($value) </h4>
<p>
一旦确定出现了错误，那么可以使用这个方法来取得相应的错误信息，下面是DB中的预定义的错误信息： </p>
<blockquote><p>
    DB_ERROR                    => &#8220;unknown error&#8221;,<br />
    DB_ERROR_ALREADY_EXISTS     => &#8220;already exists&#8221;,<br />
    DB_ERROR_CANNOT_CREATE      => &#8220;can not create&#8221;,<br />
    DB_ERROR_CANNOT_DELETE      => &#8220;can not delete&#8221;,<br />
    DB_ERROR_CANNOT_DROP        => &#8220;can not drop&#8221;,<br />
    DB_ERROR_CONSTRAINT         => &#8220;constraint violation&#8221;,<br />
    DB_ERROR_DIVZERO            => &#8220;division by zero&#8221;,<br />
    DB_ERROR_INVALID            => &#8220;invalid&#8221;,<br />
    DB_ERROR_INVALID_DATE       => &#8220;invalid date or time&#8221;,<br />
    DB_ERROR_INVALID_NUMBER     => &#8220;invalid number&#8221;,<br />
    DB_ERROR_MISMATCH           => &#8220;mismatch&#8221;,<br />
    DB_ERROR_NODBSELECTED       => &#8220;no database selected&#8221;,<br />
    DB_ERROR_NOSUCHFIELD        => &#8220;no such field&#8221;,<br />
    DB_ERROR_NOSUCHTABLE        => &#8220;no such table&#8221;,<br />
    DB_ERROR_NOT_CAPABLE        => &#8220;DB backend not capable&#8221;,<br />
    DB_ERROR_NOT_FOUND          => &#8220;not found&#8221;,<br />
    DB_ERROR_NOT_LOCKED         => &#8220;not locked&#8221;,<br />
    DB_ERROR_SYNTAX             => &#8220;syntax error&#8221;,<br />
    DB_ERROR_UNSUPPORTED        => &#8220;not supported&#8221;,<br />
    DB_ERROR_VALUE_COUNT_ON_ROW => &#8220;value count on row&#8221;,<br />
    DB_OK                       => &#8220;no error&#8221;,<br />
    DB_WARNING                  => &#8220;unknown warning&#8221;,<br />
    DB_WARNING_READ_ONLY        => &#8220;read only&#8221;</p>
</blockquote>
<h4>assertExtension($name) </h4>
<p>
动态载入PHP的数据库扩展。$name是你的PHP扩展的名称，不包含扩展名（如.dll,.so)。<br />
当你需要让PHP载入某个数据库的扩展，但是你没有权限修改php.ini的时候，可以使用这个函数。<br />
当然，DB内部也是自动调用这个方法来载入所需的PHP数据库的扩展。</p>
<p>比如：你如果需要加载oracle的扩展，那么可以这样使用：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">include_once</span> <span style="color: #0000ff;">&quot;DB.php&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> DB<span style="color: #339933;">::</span><span style="color: #004000;">assertExtension</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;php_oci8&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;oracle 8扩展加载成功！&quot;</span>；
<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">die</span> <span style="color: #0000ff;">&quot;无法加载oracle 8扩展&quot;</span>；
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>以上是在DB类中定义的&#8221;静态&#8221;方法，所谓静态方法，是指你可以不需要构建对象，就可以直接使用，并且只要你指明DB::，你可以在任何地方直接调用这些方法。 </p>
<p>在DB.php中，除了DB外，也有几个非常重要的类，在后端数据库中也使用了这些类： </p>
<p><strong>DB_Error </strong><br />
这个类是从PEAR_Error继承来的，在数据库操作中，对于出现的致命的错误，一般会抛出这个错误。<br />
构建函数：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> DB_Error<span style="color: #009900;">&#40;</span><span style="color: #000088;">$code</span> <span style="color: #339933;">=</span> DB_ERROR<span style="color: #339933;">,</span> <span style="color: #000088;">$mode</span> <span style="color: #339933;">=</span> PEAR_ERROR_RETURN<span style="color: #339933;">,</span> <span style="color: #000088;">$level</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">E_USER_NOTICE</span><span style="color: #339933;">,</span> <span style="color: #000088;">$debuginfo</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> 
<span style="color: #666666; font-style: italic;">//$code是DB错误代码，$mode决定错误处理的模式，缺省是返回，$level 错误级别， </span>
<span style="color: #666666; font-style: italic;">//$debuginfo是附加的调式信息，如刚刚执行的SQL语句等等。</span>
&nbsp;
 <span style="color: #666666; font-style: italic;">//DB_Warning </span>
<span style="color: #666666; font-style: italic;">//类似DB_Error。</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//DB_result </span>
<span style="color: #666666; font-style: italic;">//这是非常重要的类。</span></pre></div></div>

<p>当执行相应的SQL查询后，后端的数据库类将返回一个DB_result的对象实例，你可以使用这个类的方法来获得查询结果的数据。这个类实际上是后端数据库结果集的包装，这里说的方法，在实际运行中是直接调用了后端数据库的同名的方法，不过，对于我们来说，使用这个包装类会感觉更自然一些。<br />
</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> fetchRow<span style="color: #009900;">&#40;</span><span style="color: #000088;">$fetchmode</span> <span style="color: #339933;">=</span> DB_FETCHMODE_DEFAULT<span style="color: #009900;">&#41;</span></pre></div></div>

<p>取得一行数据，$fetchmod是获取数据的模式，如果没有指定，那么使用缺省方式。其余的方式我们在后面讨论 DB_Common接口的时候会详细讨论。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> fetchInto<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$arr</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fetchmode</span> <span style="color: #339933;">=</span> DB_FETCHMODE_DEFAULT<span style="color: #009900;">&#41;</span></pre></div></div>

<p>取得一行数据，同时追加到给定的$att数组中。$att是要追加到的数组，它是按照引用方式传递的。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> numCols<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>取得结果集的列数。如果出错，返回DB_Error对象。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> numRows<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>取得结果集的行数。如果出错，返回DB_Error对象。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> free<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>释放结果集所占用的资源。</p>
<p>至此，我们已经学习了DB基本的使用方法，下面我们简单复习一下，对于其中没有见到的方法，我们在后面会详细介绍： <br />
连接数据库：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
  <span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> DB<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$db_type</span>://<span style="color: #006699; font-weight: bold;">$db_user</span>@<span style="color: #006699; font-weight: bold;">$db_passwd</span>:<span style="color: #006699; font-weight: bold;">$db_host</span>/<span style="color: #006699; font-weight: bold;">$db_dbName</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$db_options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>DB<span style="color: #339933;">::</span><span style="color: #004000;">isError</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;数据库连接错误：&quot;</span><span style="color: #339933;">.</span>DB<span style="color: #339933;">::</span><span style="color: #004000;">errorMessage</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br&gt;&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #666666; font-style: italic;">/* 查询1 */</span>
  <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;select * from user_log&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>DB<span style="color: #339933;">::</span><span style="color: #004000;">isError</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;数据库错误：&quot;</span><span style="color: #339933;">.</span>DB<span style="color: #339933;">::</span><span style="color: #004000;">errorMessage</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000088;">$colCount</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">numCols</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;tr&gt;&lt;td colspan=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$colCount</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;共找到&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span> <span style="color: #004000;">numRows</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;位用户&lt;/td&gt;&lt;/tr&gt;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;tr&gt;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$colCount</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
          <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;td&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span>$<span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/td&gt;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;/tr&gt;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">free</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">disconnect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p></p>
<h3>四、 DB_Common 使用参考 </h3>
<p>
DB_Common类是一个通用的接口，DB跨数据库平台的能力是通过实现这个接口来做到的。如果你的数据库不在DB支持之列，你可以自己编写一个类继承DB_Common类，实现这些接口的函数。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>返回当前类的字符串描述，格式是类名：（phptype=&#8221;",dbsyntax=&#8221;")[connected],一般是类似于：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">DB_mysql<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>phptype<span style="color: #339933;">=</span><span style="color: #990000;">mysql</span><span style="color: #339933;">,</span>dbsyntax<span style="color: #339933;">=</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>connected<span style="color: #009900;">&#93;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> quoteString<span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>圈引一个字符串，使之在查询中能够安全地放在单引号的分界符之间。一般对于字符型的字段，我们查询的时候使用&#8221;作为分隔符，因此如果字符串中有&#8217;则会出错，这个函数把字符串中的&#8217;替换成\&#8217;.<br />
</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> provides<span style="color: #009900;">&#40;</span><span style="color: #000088;">$feature</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>指明当前DB的后端程序是否实现了给定的特性，返回布尔值。$feature是功能的明称，一般是：&#8221;prepare&#8221;, &#8220;pconnect&#8221;,&#8221;transactions&#8221;.在后端程序的构建函数中应该设置这些值。例子：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">provider</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;transactions&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//支持事务</span>
     <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//不支持事务</span>
     <span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> errorCode<span style="color: #009900;">&#40;</span><span style="color: #000088;">$nativecode</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>用于将后端数据库产生的错误代码映射到DB的通用错误代码中去。内部调用。后端数据库在构建函数中应该初始化$errorcode_map属性。例子(mysql)：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//在DB_mysql的构建函数中</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span> <span style="color: #004000;">errorcode_map</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #cc66cc;">1004</span> <span style="color: #339933;">=&gt;</span> DB_ERROR_CANNOT_CREATE<span style="color: #339933;">,</span>
            <span style="color: #cc66cc;">1005</span> <span style="color: #339933;">=&gt;</span> DB_ERROR_CANNOT_CREATE<span style="color: #339933;">,</span>
            <span style="color: #cc66cc;">1006</span> <span style="color: #339933;">=&gt;</span> DB_ERROR_CANNOT_CREATE<span style="color: #339933;">,</span>
&nbsp;
            <span style="color: #cc66cc;">1007</span> <span style="color: #339933;">=&gt;</span> DB_ERROR_ALREADY_EXISTS<span style="color: #339933;">,</span>
            <span style="color: #cc66cc;">1008</span> <span style="color: #339933;">=&gt;</span> DB_ERROR_CANNOT_DROP<span style="color: #339933;">,</span>
            <span style="color: #cc66cc;">1046</span> <span style="color: #339933;">=&gt;</span> DB_ERROR_NODBSELECTED<span style="color: #339933;">,</span>
            <span style="color: #cc66cc;">1050</span> <span style="color: #339933;">=&gt;</span> DB_ERROR_ALREADY_EXISTS<span style="color: #339933;">,</span>
            <span style="color: #cc66cc;">1051</span> <span style="color: #339933;">=&gt;</span> DB_ERROR_NOSUCHTABLE<span style="color: #339933;">,</span>
            <span style="color: #cc66cc;">1054</span> <span style="color: #339933;">=&gt;</span> DB_ERROR_NOSUCHFIELD<span style="color: #339933;">,</span>
            <span style="color: #cc66cc;">1062</span> <span style="color: #339933;">=&gt;</span> DB_ERROR_ALREADY_EXISTS<span style="color: #339933;">,</span>
            <span style="color: #cc66cc;">1064</span> <span style="color: #339933;">=&gt;</span> DB_ERROR_SYNTAX<span style="color: #339933;">,</span>
            <span style="color: #cc66cc;">1100</span> <span style="color: #339933;">=&gt;</span> DB_ERROR_NOT_LOCKED<span style="color: #339933;">,</span>
            <span style="color: #cc66cc;">1136</span> <span style="color: #339933;">=&gt;</span> DB_ERROR_VALUE_COUNT_ON_ROW<span style="color: #339933;">,</span>
            <span style="color: #cc66cc;">1146</span> <span style="color: #339933;">=&gt;</span> DB_ERROR_NOSUCHTABLE<span style="color: #339933;">,</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.xixis.net/archives/pear-create-a-middle-tier-database-applications.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pear DB 入门指南</title>
		<link>http://www.xixis.net/archives/change-pear-db-getting-started-guide-2.html</link>
		<comments>http://www.xixis.net/archives/change-pear-db-getting-started-guide-2.html#comments</comments>
		<pubDate>Tue, 09 Feb 2010 19:48:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[db_dataobject]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[pear db]]></category>
		<category><![CDATA[数据中间层]]></category>
		<category><![CDATA[面向对象]]></category>

		<guid isPermaLink="false">http://www.xixis.net/develop/php/%e8%bd%acpear-db-%e5%85%a5%e9%97%a8%e6%8c%87%e5%8d%97/</guid>
		<description><![CDATA[很久之前转的一篇文章，非常好，迁移blog的时候丢掉了，现补上，原址找不到了。   1.         简介 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;1 2.        下载、安装 Pear&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-1 3         使用Pear DB&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-1  3.1   连接，断开数据库  3.2   执行数据库  3.3   获得select的数据  3.3.1  获取数据的函数  3.3.2  选择获取数据的格式  3.3.3  设置获取数据的格式  3.3.4  控制获取数据数量  3.3.5  清除结果，释放变量  3.4   快速retrieve数据 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;2  3.5   从查询结果获得更多信息(numRows, numCols, affectedRows, tableInfo)  3.6   自动增长（Sequences）  ...]]></description>
			<content:encoded><![CDATA[<p>很久之前转的一篇文章，非常好，迁移blog的时候丢掉了，现补上，原址找不到了。<br />  <span id="more-327"></span> 1.         简介 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;1<br /> 2.        下载、安装 Pear&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-1<br /> 3         使用Pear DB&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-1<br />  3.1   连接，断开数据库<br />  3.2   执行数据库<br />  3.3   获得select的数据<br />  3.3.1  获取数据的函数<br />  3.3.2  选择获取数据的格式<br />  3.3.3  设置获取数据的格式<br />  3.3.4  控制获取数据数量<br />  3.3.5  清除结果，释放变量<br />  3.4   快速retrieve数据 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;2<br />  3.5   从查询结果获得更多信息(numRows, numCols, affectedRows, tableInfo)<br />  3.6   自动增长（Sequences）<br />  3.7   Prepare &#038; Execute/ExcuteMultiple<br />  3.8   autoCommit, commit and rollback<br /> 4.        可用方法列表 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;3<br /> 5         错误处理机制 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;3<br /> 5.1         从Pear DB Error获得错误信息<br /> 5.2         Debug Pear DB Errors<br /> 5.3        对错误采取自动处理 <br />   1.        简介<br /> 这是一部指导我们如何使用Pear DB扩展。Pear DB，提供这样一系列的类：<br />
<blockquote>          数据库抽象          高级错误处理机制          以及其它 </p></blockquote>
<p>   2.        下载、安装Pear<br /> 由于现在Pear项目仍处于紧锣密鼓的开发之中，所以得到它的最好办法就是从CVS获得（Pear DB发行包已经跟随PHP4.0.6以后版本捆绑发布）。所以，我们只需要把Pear的根目录放到<span class='wp_keywordlink_affiliate'><a href="http://www.xixis.net/archives/tag/php" title="显示php的所有日志" target="_blank">php</a></span>.ini配置文件 include_path中。也可以通过这样设置：<br />
<blockquote> _set(&#8216;include_path&#8217;, &#8216;/pear_base_dir&#8217;). </p></blockquote>
<p> 以下是strp by step示例：<br /> 存放Pear的目录：<br /> <br />
<blockquote> # cd /usr/local/lib </p></blockquote>
<p> 用“phpfi“口令登录:<br /> <br />
<blockquote> # cvs -d :pserver:cvsread@cvs.<span class='wp_keywordlink_affiliate'><a href="http://www.xixis.net/archives/tag/php" title="显示php的所有日志" target="_blank">php</a></span>.net:/repository login </p></blockquote>
<p> 用以下命令得到所有的<span class='wp_keywordlink_affiliate'><a href="http://www.xixis.net/archives/tag/pear" title="显示pear的所有日志" target="_blank">pear</a></span>文件，同时也可以用来更新已经下载的文件。其他的参数有：&#8221;today&#8221;, &#8220;last month&#8221;,等。我推荐用&#8221;last week&#8221;参数，因为一般bugs的提交和修改都是每周一次。<br />
<blockquote> # cvs -d :pserver:cvsread@cvs.php.net:/repository export -D &#8220;last week&#8221; php4/<span class='wp_keywordlink_affiliate'><a href="http://www.xixis.net/archives/tag/pear" title="显示pear的所有日志" target="_blank">pear</a></span> </p></blockquote>
<p> 编辑php.ini文件加上下面一段在include_path处：<br />
<blockquote>  /usr/local/lib/php4/<span class='wp_keywordlink_affiliate'><a href="http://www.xixis.net/archives/tag/pear" title="显示pear的所有日志" target="_blank">pear</a></span> </p></blockquote>
<p>  如果没有修改的权限，可以通过这条语句在代码中实现：<br />
<blockquote>  ini_set(&#8216;include_path&#8217;, &#8216;path_to_pear&#8217;); </p></blockquote>
<p> 获得PHP CVS的完全文档<br /> 注意Pear DB必需PHP版本4.0.4以上，而在Pear中的一些其他包如：XML Parser of the pear installer script需要PHP4.0.5以上版本。   3.        使用Pear DB<br /> 3.1         连接，断开数据库</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
 <span style="color: #666666; font-style: italic;">// The pear base directory must be in your include_path</span>
 <span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'DB.php'</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'foo'</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$pass</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'bar'</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$host</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$db_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'clients_db'</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #666666; font-style: italic;">// Data Source Name: This is the universal connection string</span>
 <span style="color: #000088;">$dsn</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;mysql://<span style="color: #006699; font-weight: bold;">$user</span>:<span style="color: #006699; font-weight: bold;">$pass</span>@<span style="color: #006699; font-weight: bold;">$host</span>/<span style="color: #006699; font-weight: bold;">$db_name</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #666666; font-style: italic;">// DB::connect will return a Pear DB object on success</span>
 <span style="color: #666666; font-style: italic;">// or a Pear DB Error object on error</span>
 <span style="color: #666666; font-style: italic;">// You can also set to TRUE the second param</span>
 <span style="color: #666666; font-style: italic;">// if you want a persistent connection:</span>
 <span style="color: #666666; font-style: italic;">// $db = DB::connect($dsn, true);</span>
 <span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> DB<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dsn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #666666; font-style: italic;">// With DB::isError you can differentiate between an error or</span>
 <span style="color: #666666; font-style: italic;">// a valid connection.</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>DB<span style="color: #339933;">::</span><span style="color: #004000;">isError</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #990000;">die</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
 <span style="color: #339933;">....</span>
 <span style="color: #666666; font-style: italic;">// You can disconnect from the database with:</span>
 <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">disconnect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>数据源(上例中的$dsn 参数)有以下允许的格式：（从Pear/DB.php的parseDSN方法复制而来）</p>
<blockquote><p>
     *  phptype: Database backend used in PHP (mysql, odbc etc.)<br />
      *  dbsyntax: Database used with regards to SQL syntax etc.<br />
      *  protocol: Communication protocol to use (tcp, unix etc.)<br />
      *  hostspec: Host specification (hostname[:port])<br />
      *  database: Database to use on the DBMS server<br />
      *  username: User name for login<br />
      *  password: Password for login<br />
      *<br />
      * The format of the supplied DSN is in its fullest form:<br />
      *<br />
      *  phptype(dbsyntax)://username:password@protocol+hostspec/database<br />
      *<br />
      * Most variations are allowed:<br />
      *<br />
      *  phptype://username:password@protocol+hostspec:110//usr/db_file.db<br />
      *  phptype://username:password@hostspec/database_name<br />
      *  phptype://username:password@hostspec<br />
      *  phptype://username@hostspec<br />
      *  phptype://hostspec/database<br />
      *  phptype://hostspec<br />
      *  phptype(dbsyntax)<br />
      *  phptype
</p></blockquote>
<p>现在支持的数据库有 (在 phptype DSN 部分):</p>
<blockquote><p>
mysql  -> MySQL<br />
 pgsql  -> PostgreSQL<br />
 ibase  -> InterBase<br />
 msql   -> Mini SQL<br />
 mssql  -> Microsoft SQL Server<br />
 oci8   -> Oracle 7/8/8i<br />
 odbc   -> ODBC (Open Database Connectivity)<br />
 sybase -> SyBase<br />
 ifx    -> Informix<br />
 fbsql  -> FrontBase
</p></blockquote>
<p>注意并不是所有数据库特征都支持，可以从根目录>/DB/STATUS 得到详细的清单。<br />
3.2         执行数据库</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
 <span style="color: #666666; font-style: italic;">// Once you have a valid DB object</span>
 <span style="color: #339933;">...</span>
 <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;select * from clients&quot;</span><span style="color: #339933;">;</span>
 <span style="color: #666666; font-style: italic;">// If the query is a &quot;SELECT&quot;, $db-&gt;query will return</span>
 <span style="color: #666666; font-style: italic;">// a DB Result object on success.</span>
 <span style="color: #666666; font-style: italic;">// Else it simply will return a DB_OK</span>
 <span style="color: #666666; font-style: italic;">// On failure it will return a DB Error object.</span>
 <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #666666; font-style: italic;">// Always check that $result is not an error</span>
 <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>DB<span style="color: #339933;">::</span><span style="color: #004000;">isError</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #990000;">die</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
 <span style="color: #339933;">....</span>
 <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>3.3         获得select的数据<br />
3.3.1   获取数据的函数</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
 <span style="color: #666666; font-style: italic;">// Once you have a valid DB Result object</span>
 <span style="color: #339933;">...</span>
 <span style="color: #666666; font-style: italic;">// Get each row of data on each iteration until</span>
 <span style="color: #666666; font-style: italic;">// there is no more rows</span>
 <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchRow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
 <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>除了fetchRow()还可以使用fetchInto()直接插入$row的值。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
 <span style="color: #339933;">...</span>
 <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchInto</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
 <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>3.3.2   选择获取数据的格式<br />
获取模式有</p>
<blockquote><p>
DB_FETCHMODE_ORDERED（默认）<br />
DB_FETCHMODE_ASSOC<br />
DB_FETCHMODE_OBJECT
</p></blockquote>
<p>从获取数据方法返回的结果示例：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
 <span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'select id, name, email from users'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchRow</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mode</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #666666; font-style: italic;">//With $mode = DB_FETCHMODE_ORDERED</span>
 <span style="color: #666666; font-style: italic;">//The default behavior is to return an ordered array.</span>
 <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span>
     <span style="color: #cc66cc;">0</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">&lt;</span>column <span style="color: #0000ff;">&quot;id&quot;</span> data<span style="color: #339933;">&gt;,</span>
     <span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">&lt;</span>column <span style="color: #0000ff;">&quot;name&quot;</span> data<span style="color: #339933;">&gt;,</span>
     <span style="color: #cc66cc;">2</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">&lt;</span>column <span style="color: #0000ff;">&quot;email&quot;</span> data<span style="color: #339933;">&gt;</span>
 <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #666666; font-style: italic;">//With $mode = DB_FETCHMODE_ASSOC</span>
 <span style="color: #666666; font-style: italic;">//Returns an associative array with column names as array keys:</span>
 <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span>
     <span style="color: #0000ff;">'id'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">&lt;</span>column <span style="color: #0000ff;">&quot;id&quot;</span> data<span style="color: #339933;">&gt;,</span>
     <span style="color: #0000ff;">'name'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">&lt;</span>column <span style="color: #0000ff;">&quot;name&quot;</span> data<span style="color: #339933;">&gt;,</span>
     <span style="color: #0000ff;">'email'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">&lt;</span>column <span style="color: #0000ff;">&quot;email&quot;</span> data<span style="color: #339933;">&gt;</span>
 <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
 <span style="color: #666666; font-style: italic;">//With $mode = DB_FETCHMODE_OBJECT</span>
 <span style="color: #666666; font-style: italic;">//Returns a DB_row object with column names as properties:</span>
 <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> db_row Object 
 <span style="color: #009900;">&#40;</span>
     <span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">&lt;</span>column <span style="color: #0000ff;">&quot;id&quot;</span> data<span style="color: #339933;">&gt;,</span>
     <span style="color: #009900;">&#91;</span>name<span style="color: #009900;">&#93;</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">&lt;</span>column <span style="color: #0000ff;">&quot;name&quot;</span> data<span style="color: #339933;">&gt;,</span>
     <span style="color: #009900;">&#91;</span>email<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #339933;">&lt;</span>column <span style="color: #0000ff;">&quot;email&quot;</span> data<span style="color: #339933;">&gt;</span>
 <span style="color: #009900;">&#41;</span>
&nbsp;
 <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #339933;">;</span>
 <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>3.3.3   设置获取数据的格式<br />
可以使用 fetchrow() / fetchInto() 方法或者为你的DB实例设置一个默认的模式。</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
 <span style="color: #339933;">...</span>
 <span style="color: #666666; font-style: italic;">// 1) Set the mode per call:</span>
 <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchRow</span><span style="color: #009900;">&#40;</span>DB_FETCHMODE_ASSOC<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #009900;">&#91;</span><span style="color: #339933;">..</span><span style="color: #009900;">&#93;</span>
 <span style="color: #009900;">&#125;</span>
 <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchInto</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #339933;">,</span> DB_FETCHMODE_ASSOC<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #009900;">&#91;</span><span style="color: #339933;">..</span><span style="color: #009900;">&#93;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
 <span style="color: #666666; font-style: italic;">// 2) Set the mode for all calls:</span>
 <span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> DB<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dsn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #666666; font-style: italic;">// this will set a default fetchmode for this Pear DB instance</span>
 <span style="color: #666666; font-style: italic;">// (for all queries)</span>
 <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFetchMode</span><span style="color: #009900;">&#40;</span>DB_FETCHMODE_ASSOC<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchRow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
 <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>3.3.4   控制获取数据数量<br />
同时Pear DB获取数据可以带有额外的参数，可以使用一个数字参数来获取需要的数据数量。在你只需要获得数据中的一部分时这时候特别有用（比如在做分页程序的时候）</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
 <span style="color: #339933;">...</span>
 <span style="color: #666666; font-style: italic;">// the row to start fetching</span>
 <span style="color: #000088;">$from</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">50</span><span style="color: #339933;">;</span>
 <span style="color: #666666; font-style: italic;">// how many results per page</span>
 <span style="color: #000088;">$res_per_page</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
 <span style="color: #666666; font-style: italic;">// the last row to fetch for this page</span>
 <span style="color: #000088;">$to</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$from</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$res_per_page</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">range</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$from</span><span style="color: #339933;">,</span> <span style="color: #000088;">$to</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$rownum</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$res</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchrow</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fetchmode</span><span style="color: #339933;">,</span> <span style="color: #000088;">$rownum</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
     <span style="color: #339933;">....</span>
 <span style="color: #009900;">&#125;</span>
 <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>3.3.5   清除结果，释放变量<br />
当你完成查询的时候，可以用free()方法来结束: </p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
 <span style="color: #339933;">...</span>
 <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SELECT * FROM clients'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchRow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #339933;">...</span>
 <span style="color: #009900;">&#125;</span>
 <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">free</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.xixis.net/archives/change-pear-db-getting-started-guide-2.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[原创]以 pear db开发基类创建数据中间层便利对数据库的操作</title>
		<link>http://www.xixis.net/archives/change-pear-db-getting-started-guide.html</link>
		<comments>http://www.xixis.net/archives/change-pear-db-getting-started-guide.html#comments</comments>
		<pubDate>Tue, 09 Feb 2010 19:04:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[db_dataobject]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[pear db]]></category>
		<category><![CDATA[面向对象]]></category>

		<guid isPermaLink="false">http://www.xixis.net/develop/php/%e8%bd%acpear-db-%e5%85%a5%e9%97%a8%e6%8c%87%e5%8d%97/</guid>
		<description><![CDATA[受到pear db_object的启发，觉得是否可以自己创建符合自己项目的基类，通过继承可以快速的实现对说据库相应表的操作方法。    首先定义数据库联接文件conn.inc.php

&#60;?php
/*
 *　用于数据的的联接操作，采用Pear的DB来操纵数据库
 */
  require_once &#34;DB.php&#34;;
  $user = 'root';//用户名
  $pass = '';//联连密码
  $host = 'localhost';//服务器名
  $db_name = 'db_name1';//数据库名
  $dsn = &#34;mysql://$user:$pass@$host/$db_name&#34;;//
  $B_DB = DB::connect&#40;$dsn&#41;;//数据库连接操作
&#160;
  if&#40;DB::isError&#40;$B_DB&#41;&#41;&#123;
  //连接错误处理
    echo &#34;An error occurred while trying to connect to the database server.
\n&#34;;
          echo &#34;Error message: &#34; . $B_DB-&#62;getMessage&#40;&#41; . &#34;
\n&#34;;
          echo &#34;A more detailed error description: &#34;;
    ...]]></description>
			<content:encoded><![CDATA[<p>受到<span class='wp_keywordlink_affiliate'><a href="http://www.xixis.net/archives/tag/pear" title="显示pear的所有日志" target="_blank">pear</a></span> db_object的启发，觉得是否可以自己创建符合自己项目的基类，通过继承可以快速的实现对说据库相应表的操作方法。  <span id="more-323"></span>  首先定义数据库联接文件conn.inc.<span class='wp_keywordlink_affiliate'><a href="http://www.xixis.net/archives/tag/php" title="显示php的所有日志" target="_blank">php</a></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?<span class='wp_keywordlink_affiliate'><a href="http://www.xixis.net/archives/tag/php" title="显示php的所有日志" target="_blank">php</a></span></span>
<span style="color: #666666; font-style: italic;">/*
 *　用于数据的的联接操作，采用Pear的DB来操纵数据库
 */</span>
  <span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">&quot;DB.<span class='wp_keywordlink_affiliate'><a href="http://www.xixis.net/archives/tag/php" title="显示php的所有日志" target="_blank">php</a></span>&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'root'</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//用户名</span>
  <span style="color: #000088;">$pass</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//联连密码</span>
  <span style="color: #000088;">$host</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//服务器名</span>
  <span style="color: #000088;">$db_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'db_name1'</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//数据库名</span>
  <span style="color: #000088;">$dsn</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;mysql://<span style="color: #006699; font-weight: bold;">$user</span>:<span style="color: #006699; font-weight: bold;">$pass</span>@<span style="color: #006699; font-weight: bold;">$host</span>/<span style="color: #006699; font-weight: bold;">$db_name</span>&quot;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//</span>
  <span style="color: #000088;">$B_DB</span> <span style="color: #339933;">=</span> DB<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dsn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//数据库连接操作</span>
&nbsp;
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>DB<span style="color: #339933;">::</span><span style="color: #004000;">isError</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$B_DB</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//连接错误处理</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;An error occurred while trying to connect to the database server.
<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
          <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Error message: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$B_DB</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;
<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
          <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;A more detailed error description: &quot;</span><span style="color: #339933;">;</span>
          <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$B_DB</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDebugInfo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>基类文件：BaseClass.php 非完整版本，读者可依自己项目特点抽象出适合的方法；</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>php<span style="color: #339933;">&lt;</span>br <span style="color: #339933;">/&gt;</span><span style="color: #666666; font-style: italic;">/*
***********************************************
*        BaseClass.php
*        Base Class of the DB Execute function
*        Version  : 1.0
*        Author   : zhxiyao (zhxiyao#gmail(dot)com)
*        Copyright: www.xixis.net
*        Writed   : 2007/07/20
*        Modified : -
***********************************************
* BaseClass作为数据库操作的基类
* 包含了大多数对数据库操作的方法
* 不要直接实例化本类，要通过继承的方法来使用
* 可以在子类中将个别方法复写以适应特定的表
***********************************************/</span>
<span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;./conn.ini.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">class</span> BC_BaseClass<span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$BV_TableName</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//对象表名在继承时负值</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$BV_SelectByID</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$DelbyID</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$BV_Insert</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">//继承时需负值</span>
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$BV_Update</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">//继承时需负值</span>
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$Error</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">/*
            构造函数初始化成员变量
        */</span>
        <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">BV_SelectByID</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;select * from &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">BV_TableName</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; where ID=?&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">BV_DelbyID</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'delete from '</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">BV_TableName</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' where ID = ?'</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">/*
            function  BF_SelectByID;
            参数: $ID
            查询ID为$ID的记录
            输出: 查询结果集 为Array
        */</span>
        <span style="color: #000000; font-weight: bold;">function</span> BF_SelectByID<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ID</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$B_DB</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$B_DB</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">BV_SelectByID</span><span style="color: #339933;">,</span><span style="color: #000088;">$ID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>PEAR<span style="color: #339933;">::</span><span style="color: #004000;">isError</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;数据库错误: &quot;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;错误信息: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$array</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;详细信息: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$array</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDebugInfo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
                <span style="color: #000088;">$array</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchInto</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #339933;">,</span>DB_FETCHMODE_ASSOC<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Error</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;没有相关的记录&quot;</span><span style="color: #339933;">;</span>
                        <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #000088;">$row</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">/*
            function  BF_SelectBySql;
            参数: $SQL
            查询SQL为$SQL的记录
            输出: 查询结果集 为Array
        */</span>
        <span style="color: #000000; font-weight: bold;">function</span> BF_SelectBySql<span style="color: #009900;">&#40;</span><span style="color: #000088;">$SQL</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$B_DB</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$B_DB</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$SQL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>DB<span style="color: #339933;">::</span><span style="color: #004000;">isError</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Error</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
                <span style="color: #000088;">$rows</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fetchInto</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #339933;">,</span>DB_FETCHMODE_ASSOC<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$rows</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rows</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Error</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;无记录.&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$rows</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">/*
            function  BF_Insert;
            参数: $InsertArray
            插入SQL为$Array的记录
            输出: 结果集 为true 或者false
        */</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">function</span> BF_Insert<span style="color: #009900;">&#40;</span><span style="color: #000088;">$InsertArray</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$B_DB</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$B_DB</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Prepare</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">BV_Insert</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$B_DB</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #339933;">,</span><span style="color: #000088;">$InsertArray</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>PEAR<span style="color: #339933;">::</span><span style="color: #004000;">isError</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;数据库错误: &quot;</span> <span style="color: #339933;">.:</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;错误信息: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;详细信息: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDebugInfo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">/*
            function  BF_Update;
            参数: $UpdateArray,$ID
            插入对象为$UpdateArray的记录
            输出: 结果集 为true 或者false
        */</span>
        <span style="color: #000000; font-weight: bold;">function</span> BF_Update<span style="color: #009900;">&#40;</span><span style="color: #000088;">$UpdateArray</span><span style="color: #339933;">,</span><span style="color: #000088;">$ID</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$B_DB</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$temp</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$temp</span><span style="color: #339933;">=</span><span style="color: #000088;">$UpdateArray</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$temp</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #000088;">$ID</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$B_DB</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Prepare</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">BV_Update</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
           <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$B_DB</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #339933;">,</span><span style="color: #000088;">$temp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
           <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>PEAR<span style="color: #339933;">::</span><span style="color: #004000;">isError</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;数据库错误: &quot;</span> <span style="color: #339933;">.:</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;错误信息: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;详细信息: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDebugInfo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
           <span style="color: #009900;">&#125;</span>
           <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #666666; font-style: italic;">/*
            function  BF_DeleteByID;
            参数: $ID
            插入对象为$Array的记录
            输出: 结果集 为true 或者false
        */</span>
        <span style="color: #000000; font-weight: bold;">function</span> BF_DeleteByID<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ID</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$B_DB</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$B_DB</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">prepare</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">BV_DelbyID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$B_DB</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #339933;">,</span><span style="color: #000088;">$ID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>DB<span style="color: #339933;">::</span><span style="color: #004000;">isError</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Error</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;数据库错误: &quot;</span> <span style="color: #339933;">.:</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
                	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;错误信息: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
                	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;详细信息: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDebugInfo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
                        <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$B_DB</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">affectedRows</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">!=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Error</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'该记录不存在'</span><span style="color: #339933;">;</span>
                        <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'该记录不存在'</span><span style="color: #339933;">;</span>
                        <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>基类的实例化，加入操作对象是数据库db_name1中的test表：</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`test`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`ID`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`Name`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`Bak1`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`Bak2`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`ID`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> TYPE<span style="color: #66cc66;">=</span>MyISAM <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span> ;</pre></div></div>

<p>建立包含支持对test表操作的类文件C_Test.php;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #009933; font-style: italic;">/**
 *类名称:BC_Test
 *类方法:
  继承自基类的方法：
 *  BF_SelectByID
 *  BF_SelectBySql
 * BF_Insert
 * BF_Update
&nbsp;
 * 类开发目标:定义封装数据库中Test表所有操作
 * 作者:zhxiyao
 *
 * @version v1.0 //每修改一次,版本增加小号1.1,大的修改变为2.0
 * 每进行一次修改,一定要在开始说明修改原因,修改后性能的改进
 * @copyright 2007
 */</span>
        <span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;./BaseClass.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">class</span>   BC_Test <span style="color: #000000; font-weight: bold;">extends</span> BC_BaseClass<span style="color: #009900;">&#123;</span>
&nbsp;
                <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">BV_TableName</span> <span style="color: #339933;">=</span><span style="color: #0000ff;">'test'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//指定操作表名；</span>
                        <span style="color: #666666; font-style: italic;">//初始化表含有的字段；</span>
                        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">BV_Insert</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'INSERT INTO `test` (`Name` , `Bak1` , `Bak2`) VALUES (?,?,?)'</span><span style="color: #339933;">;</span> 
                        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">BV_Update</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'UPDATE `product` SET `Name` = ?,`Bak1` = ?,`Bak2` = ?  WHERE `ID` = ? '</span><span style="color: #339933;">;</span>
&nbsp;
                        parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>                       
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000000; font-weight: bold;">function</span> 新方法<span style="color: #009900;">&#123;</span>
&nbsp;
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000000; font-weight: bold;">function</span> 基类方法名<span style="color: #009900;">&#123;</span>
                   <span style="color: #666666; font-style: italic;">//复写方法</span>
                <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>以此便可快速的实现包含对数据库对应表操作的类。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.xixis.net/archives/change-pear-db-getting-started-guide.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

