background image

确定得到的记录笔数,会回传

 -1 。

范例

 2: 进阶的 Select 指令(使用 Field 对象)

任务:选取一个资料表,显示最前面的二栏。如果第二栏是一个日期或时间型态字段,将它
格式化成

US 格式。

<?
include('ADOdb.inc.php');       
$conn = &ADONewConnection('access');    
$conn->PConnect('northwind');   
$recordSet = &$conn->Execute('select CustomerID,OrderDate from Orders');
if (!$recordSet) 
        print $conn->ErrorMsg();
else
while (!$recordSet->EOF) {
        $fld = $recordSet->FetchField(1);
        $type = $recordSet->MetaType($fld->type);
 
        if ( $type == 'D' || $type == 'T')
                print $recordSet->fields[0].' '.
                        $recordSet->UserDate($recordSet->fields[1],'m/d/Y').'<BR>';
        else
                print $recordSet->fields[0].' '.$recordSet->fields[1].'<BR>';
 
        $recordSet->MoveNext();
}
$recordSet->Close(); # optional
$conn->Close(); # optional
 
?>
在这个例子中,我们使用

 

FetchField

() 函数来检查第二个字段的资料型别。这将会回传一个

至少有三个字段的对象,字段说明如下:
·         name: 字段名
·         type: 字段的资料原生型别 native field type of column
·         max_length: 字段的最大长度,部份数据库像 MySQL,并不回传字段的正确值,以这
个例子而言,就会回传

 -1 。

然后我们使用

 

MetaType

() 去转换原生型别成通用型别,目前通用型别定义如下:

·         C:  character 字段,应该使用 <input type="text"> 标记来取值。
·         X: 文字字段(Text) , 长文字字段,使用 <textarea> 标记来显示资料。
·         B: Blob 字段或者大型的二位对象(像程序,图文件等)。
·         D: 日期字段
·         T: 时间字段
·         L: 逻辑字段(真假值)或位字段
·         N: 数字字段,包含自动进位、编号、整数、浮点数、实数等。
·         R: 序列字段,包含了序列、自动增进整数,只对被选择的数据库作用。
如果对应型别是日期或时间,那你可以使用

 

UserDate

() 函数来设定输出的日期格式。这个函