PEAR:创建中间的数据库应用层2
创建|数据|数据库
function errorMessage($dbcode)
返回DB错误文本信息。
function &raiseError($code = DB_ERROR, $mode = false, $level = false,$debuginfo = false, $nativecode = false)
抛出一个错误。这个函数由DB来调用。
function setFetchMode($fetchmode)
设置缺省的fetch模式。
fetchmod有以下几种:
DB_FETCHMODE_DEFAULT :使用缺省的模式
DB_FETCHMODE_ORDERED :每条记录的数据列使用数字索引,从0开始
DB_FETCHMODE_ASSOC :每条记录的数据列使用字段名索引,同查询中的字段名一致
DB_FETCHMODE_FLIPPED:如果结果集是多维的,多条记录多个字段,一般来说返回一个2维数组,第一维是记录号,标明是第几条记录,第2维的数组则使用字段名或数字索引。DB_FETCHMODE_FLIPPED则会交换这个顺序,也就是说,第一层是字段名,第2维才是记录号.
function setOption($option, $value)
设置后端数据库选项。$options,$value分别是选项名和相应的值。
一般不用直接调用,在DB_Common及其子类的构建函数中会调用这个函数。
function getOption($option)
取得某个option的值
function prepare($query)
为execute()准备编译一个查询。对于某些后端数据库,这是通过仿真来实现的。返回编译后的查询句柄,出错则返回一个DB_Error对象。
function execute($stmt, $data = false)
执行编译后的查询。$stmt是由prepare返回的句柄。$data是参数数据,如果你使用的是参数查询,$data将要包含每个?参数的值
例子:
/**
* 下面是执行一个删除查询,从文章表中将指定用户的文章记录删除
*/
$sql = "delete from artilce where article.userid =$userid";
$qh = $db->prepare($sql);
if(DB::isError($qh)){
return $qh;
}
$result = $db->execute($qh);
return $result;
?>
function executeEmulateQuery($stmt, $data = false)
返回一个实际的查询字符串,供仿真prepare,execute的时候使用,内部函数。如果出错,则返回DB_Error对象
function executeMultiple( $stmt, &$data )
在同一个查询句柄上执行多个查询。$data必需是一个从0开始的数组,这个函数将 依次使用数组中的每一行数据来调用execute。这个函数一般用于参数查询。你可执行一次 查询的编译,然后将不同的参数值放入$data数组,然后就可一次同时执行查询了。需要注意,如果中间某个查询出错,剩余的查询不会继续进行而是返回错误。
function modifyQuery($query)
内部函数,用于后端数据库修正查询,后端数据库实现这个函数,然后返回进行优化和修正后查询串。例子:这是DB_mysql的实现,
function modifyQuery($query)
{
if ($this->options['optimize'] == 'portability') {
// "DELETE FROM table" gives 0 affected rows in MySQL.
// This little hack lets you know how many rows were deleted.
if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $query)) {
$query = preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/',
'DELETE FROM \1 WHERE 1=1', $query);
}
}
return $query;
}
?>
function &query($query)
执行一个查询,查询成功,如果是有结果集的查询,则回一个DB_Result对象,如果没有结果集的查询则返回DB_OK。
出错则返回DB_Error对象。
function &getOne($query, $params = array())
执行查询,并返回结果集中第一行第一列的数据。$params是参数值,如果后端数据库支持,将调用prepare/execute来使用这些参数。
例子:
$sql = "select id,date from article where userid= $userid orderby date";
$last = $db->getOne($sql);
if(DB::isError($last)){
echo "出错:".DB::errorMessage($last);
}
echo "id:".$last;
?>
function &getRow($query, $fetchmode = DB_FETCHMODE_DEFAULT, $params = array())
执行查询,请返回结果集的第一条记录。
$fetchmod 指定fetch模式,如果省略,使用缺省模式。
例子:
$sql = "select * from articles order by date desc";
$row = $db->getRow($sql);
if(DB::isError($row)){
echo "出错:".DB::errorMessage($row);
}
for($i=0;$i
}
?>
function &getCol($query, $col = 0, $params = array())
执行查询,并返回包含结果集中指定字段列的值的数组。
$col是要返回的列的索引,可以是整数,或者是关键字段名。
例子:
$sql = "select id,userid,date from articles order by date desc limit 100";
//只返回用户列表
$row = $db->getCol($sql,1);
if(DB::isError($row)){
echo "出错:".DB::errorMessage($row);
}
for($i=0;$i
}
?>
function &getAssoc($query, $force_array = false, $params = array())
执行查询,并返回一个关联数组。
$force_arry 强制返回数组。如果true,那么即使你的结果集里只有2个字段,那么关键字段对应的值也是一个只有一个元素的
数组。如果false,那么关键字段对应的值是一个标量了。
注意,这个关联数组有些特别:
如果你查询的是"select userid,name,reg_date from user",记录集是:
userid name reg_date
test testor 2001-07-05
test2 teest2 2001-07-06
那么返回的数组是这样的:
array( 'test' => array('testor','2001-0705'),
'test2'=> array('teest2','2001-07-06'
)
例子:
$sql = "select userid,name,reg_date,last_login from users limit 100";
$userinfo = $db->getAssoc($sql);
if(DB::isError($userinfo)){
die "错误!".DB::errorMessage($userinfo);
}
if(empty($userinfo)){
echo "warning:NO users!";
return;
}
/*现在,userinfo里面保存有用户的信息*/
function getUserInfo($user_id=''){
$info = $userinfo[$user_id];
if (empty($info){
echo "没有这个用户信息!";
}
return $info;
}
?>
function &getAll($query, $fetchmode = DB_FETCHMODE_DEFAULT, $params = array())
返回包含结果集中全部记录的数组。注意,如果你的结果集很大,不要使用这个函数。
例子:
$sql ="select * from users limit 1000";
$list = $db->getAll($sql);
if(DB::isError($list)){
die "数据库错误:".DB::errorMessage($list);
}
for ($i=0;$i
echo "
for($j=0;$j
}
echo "
}
?>
function autoCommit($onoff=false)
指定是否自动提交事务。有的后端数据库不支持。
function commit()
提交当前事务
function rollback()
回滚当前事务
function numRows($result)
返回结果集中的记录数
function affectedRows()
返回上一次查询操作的记录数
function nextId($seq_name, $ondemand = true)
返回指定的sequence的下一个值
function createSequence($seq_name)
创建一个Sequence
function dropSequence($seq_name)
删除一个Sequence
五、 更进一步,创建你自己的中间数据库应用层
到此,我们对于DB类的功能已经有了更深的了解。我们可以基于DB类来创建你自己的数据库应用层了。也许你会问,我为什么还要创建新的类,直接在我的应用程序中使用DB不好么?答案是,当然可以,但是我不推荐。
首先,虽然DB的功能很强大,但是仍然是过于底层的,你的类应该是面向应用的。你的这个数据库层应该屏蔽不需要使用的功能和函数,同时,也要提供一些更'高级'的方法,比如,你的应用程序不应该去联接数据库,释放资源,这些应该是透明的。那么这些工作就要由你的这个类来完成了。
其次,DB仍有一些缺陷,一旦找到比它更好的,你可以迅速地升级。你所要改的只是这个类的方法如何实现,你的应用程序的其他模块不会为此受到影响。
在你设计自己的类的时候,希望能够一些准则:
提供基本的自由的查询接口。包括query,execute.分别对应有结果集和无结果集的查询。
不要使用特定数据库的某些特性,即使因为放弃使用这些特征会给你增加不少的代码量。
尽量屏蔽一些数据库的操作细节,比如连接数据库,释放资源等等。
六、 DB的不足
上面我们讨论了DB的优点和一些使用的方法与技巧。但是,任何事物都不是十全十美的,DB类更是如此,由于DB乃至PEAR的开发时间并不长,因此DB并没有最终全部完成,其中也或多或少地存在一些BUG或者设计上的问题,需要我们在使用中去发现和修正。
MYSQL的问题
问题1:前段我在开发一个项目中,发现DB的MYSQL类有一个问题,那就是当你connect的时候,MYSQL类自动将当前数据库设置为$DSN中的数据库名。以后使用query的时候,都是使用当前数据库。下面是原来connect的代码:
if ($dsninfo["database"]) {
if (!mysql_select_db($dsninfo["database"], $conn)) {
return $this->raiseError(); // XXX ERRORMSG
}
}
这导致了一个后来令我费解的问题:
我的项目需要我连接2个数据,假设分别是user和test。test是我的主要数据库,但是我要从第一个数据库中user中取得用户信息,同时test中保存用户的权限信息。我为此做了一个中间的类CV_DB,用来实现我的数据库层。在CV_DB的创建函数中,我连接到指定的数据库,从而我可以这样使用:
$db1 = new CV("user");
$db2 = new CV("test");
后面当我执行,查询某个用户的信息的时候,出现了"该表不存在"的DB错误。但实际上这个表是存在的。最后,我发现原来是DB/mysql.php的问题,因为它的查询使用的是,mysql_query,而不是mysql_db_query,当我连接到第2个数据库的时候,MYSQL的当前数据库变成了第2个,这时我再执行针对第一个数据库的查询,当然会出错。
解决方式有2个,在创建CV的时候不连接到数据库,查询的时候连接,查询完毕后断开。或者,修改DB/Mysql.php。我选择后者,我将上面几行注释,然后将SimpleQuery中的mysql_query 替换成mysql_db_query.
问题2:mysql没有execute,因此它继承使用了DB_common中模拟方式,但是不幸地是,这带来了新的问题,在一些更新查询中,所要更新的数据有? &这2个特殊字符的时候,会出现问题,因为prepare认为这是参数查询的字符,将进行分析,导致无法更新数据。
解决方式也有2个:替换?和&,但是这样要考虑的事情很多。或者:直接使用simpleQuery或者query。
我选择后者,由于我的其他类只和CV――我这个中间数据库应用类打交道,于是,我在CV的execute方法中做了判断,如果是后端数据库是mysql,那么我直接调用simpleQuery,否则,调用后端数据库的prepare和execute。这样,实际的后端数据库对于我项目中的其他应用类是透明的,我可以简单地做相应的调整,这也是我设计数据库应用层的初衷。
DB的开发状态
DB目前仍在不断地开发当中,在DB/下面有一个文件STATUS,它描述了DB类的功能和各个后端数据库的实现情况,下面是PHP4.0.6这个发布中的开发情况:
"x" - 已经实现,但尚未测试implemented, but without tests
"t" - 已经实现,但是一个或多个测试没有通过implemented, but one or more tests fail
"T" - 实现并通过全部测试implemented, passing all tests
"e" - 仿真实现,没有测试emulated, without tests
"E" - 仿真实现,通过全部测试emulated, passing all tests
"n" - 返回 "not capable",没有这个能力提供该项功能
"-" - 没有实现
fbsql ifx mssql oci8 pgsql
FEATURE | ibase | msql | mysql | odbc | sybase
simpleQuery x x x x x T T x T x
numCols x x x x x T T x T x
numRows x n n x x T E n T n
errorNative x n x n n T x x T n
prepare/execute e x e e e E T e E e
sequences e n n n n E T n T n
affectedRows x n x n n T T n T n
fetch modes x x x x x T T x T x
fetch absolute rows x n x x x x n x x x
transactions x x n n n n x x x n
auto-commit x x n n n n x x x n
error mapping x - e - - T T x E -
tableInfo x n n n n T n n n n
七、参考资源
ADODB另一个非常好操纵数据库的PHP程序,DB的绝好的替代者
关于作者
潘凡(Night Sailer):北京赛迪数据有限公司工程师。主要研究兴趣是Perl,PHP与XML的应用,PHP的MVC开发模式,PERL-GTK的使用。您可以通过 E-mail:nightsailer@hotmail.com 跟他联系。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

The activation process on Windows sometimes takes a sudden turn to display an error message containing this error code 0xc004f069. Although the activation process is online, some older systems running Windows Server may experience this issue. Go through these initial checks, and if they don't help you activate your system, jump to the main solution to resolve the issue. Workaround – close the error message and activation window. Then restart the computer. Retry the Windows activation process from scratch again. Fix 1 – Activate from Terminal Activate Windows Server Edition system from cmd terminal. Stage – 1 Check Windows Server Version You have to check which type of W you are using
