Home  >  Article  >  php教程  >  Adodb的十个实例(清晰版)

Adodb的十个实例(清晰版)

WBOY
WBOYOriginal
2016-06-13 12:34:51853browse

本想学pear的,可是网上看到的几篇帖子对adodb的评价相当高,所以改学了这个。 
ADODB的优点有这几个(网上说的,不是我说的): 
1、速度比pear快一倍; 
2、支持的数据库类型比pear多很多,甚至可以支持ACCESS; 
3、无须安装,无须服务器支持(对新手来说,这点很重要吧) 
不知道adodb是什么或是想下载adodb的朋友可以去这个链接看看:http://www.phpe.net/class/106.shtml  
另外,如果哪位兄弟翻译了README的全文或知道哪里有译文请给我回个帖,谢谢。 
Tutorial  
Example 1: Select Statement  
任务: 连接一个名为Northwind的Access数据库, 显示 每条记录 的前两个字段.  

在这个实例里, 我们新建了一个ADOC连接(ADOConnection)对象, 并用它来连接一个数据库. 这个连接采用PConnect 方法, 这是一个持久 连接. 当我们要查询数据 库时, 我们可以随时调 用这个连接的Execute()函数. 它会返回一个ADORecordSet对象 which is actually a cursor that holds the current row in the array fields[]. 我们使用MoveNext()从一个记录转向下一个记录 .  

NB: 有一 个非常实用的函数 SelectLimit在本例中没有用到, 它可以控制显示的记录数(如只显示前十条记录 ,可用作分页显示 ).  


PHP:-------------------------------------------------------------------------------- 

  
include('adodb.inc.php'); #载入ADOdb  
$conn = &ADONewConnection('access'); # 新建一个连接  
$conn->PConnect('northwind'); # 连接到一个名为northwind的MS-Access数据库  
$recordSet = &$conn->Execute('select * from products'); #从products数据表中搜索所有数据  
if (!$recordSet)  
print $conn->ErrorMsg(); //如果数据搜索发生错误显示错误信息  
else  
while (!$recordSet->EOF) {  
print $recordSet->fields[0].' '.$recordSet->fields[1].'
';  
$recordSet->MoveNext(); //指向下一个记录  
} //列表显示数据  

$recordSet->Close(); //可选  
$conn->Close(); //可选  
?>  
-------------------------------------------------------------------------------- 

$recordSet在$recordSet->fields中返回当前数组, 对字段进行数字索引(从0开始). 我们用MoveNext() 函数移动到下一个记录 . 当数据库搜索到结尾时EOF property被 设置 为true. 如果Execute()发生错误 , recordset返回flase.  

$recordSet->fields[]数组产生于PHP的数据库扩展。有些数据库扩展只能按数字索引而不能按字段名索引.如果坚持要使用字段名索引,则应采用SetFetchMode函数.无论采用哪种格式索引,recordset都可以由Execute()或SelectLimit()创建。  


PHP:-------------------------------------------------------------------------------- 
$db->SetFetchMode(ADODB_FETCH_NUM);  
$rs1 = $db->Execute('select * from table'); //采用数字索引  
$db->SetFetchMode(ADODB_FETCH_ASSOC);  
$rs2 = $db->Execute('select * from table'); //采用字段名索引  
print_r($rs1->fields); # shows array([0]=>'v0',[1] =>'v1')  
print_r($rs2->fields); # shows array(['col1']=>'v0',['col2'] =>'v1')-------------------------------------------------------------------------------- 


如果要获取记录号,你可以使用$recordSet->RecordCount()。如果没有当前记录则返回-1。  

实例 2: Advanced Select with Field Objects  
搜索表格,显示前两个字段. 如果第二个字段是时间或日期格式,则将其改为美国标准时间格式显示.  

PHP:-------------------------------------------------------------------------------- 

  
include('adodb.inc.php'); ///载入adodb  
$conn = &ADONewConnection('access'); //新建一个连接  
$conn->PConnect('northwind'); //连接名为northwind的MS-Access数据库  
$recordSet = &$conn->Execute('select CustomerID,OrderDate from Orders'); //从Orders表中搜索CustomerID和OrderDate两个字段  
if (!$recordSet)  
print $conn->ErrorMsg(); //如果数据库搜索错误,显示错误信息  
else  
while (!$recordSet->EOF) {  
$fld = $recordSet->FetchField(1); //把第二个字段赋值给$fld  
$type = $recordSet->MetaType($fld->type); //取字段值的格式  

if ( $type == 'D' || $type == 'T')  
print $recordSet->fields[0].' '.  
$recordSet->UserDate($recordSet->fields[1],'m/d/Y').'
'; //如果字段格式为日期或时间型,使其以美国标准格式输出  
else  
print $recordSet->fields[0].' '.$recordSet->fields[1].'
'; //否则以原样输出  

$recordSet->MoveNext(); //指向下一个记录  
}  
$recordSet->Close(); //可选  
$conn->Close(); //可选  

?>  
-------------------------------------------------------------------------------- 

在这个例子里, 我们用FetchField()函数检查了第二个字段的格式. 它返回了一个包含三个变量的对象  

name: 字段名  
type: 字段在其数据库中的真实格式  
max_length:字段最大长度,部分数据库不会返回这个值,比如MYSQL,这种情况下max_length值等于-1.  
我们使用MetaType()把字段的数据库格式转化为标准的字段格式  

C: 字符型字段,它应该可以在标签下显示.  
X: 文本型字段,存放比较大的文本,一般作用于

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn