adodb,设置rs从数据库中提取数据的索引是数字还是字段名

$ADODB_FETCH_MODE

This is a global variable that determines how arrays are retrieved by recordsets. The recordset saves this value on creation (eg. in Execute( ) or SelectLimit( )), and any subsequent changes to $ADODB_FETCH_MODE have no affect on existing recordsets, only on recordsets created in the future.

The following constants are defined:

define('ADODB_FETCH_DEFAULT',0);
define('ADODB_FETCH_NUM',1);
define('ADODB_FETCH_ASSOC',2);
define('ADODB_FETCH_BOTH',3);

An example:

    $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
    
$rs1 = $db->Execute('select * from table');
    
$ADODB_FETCH_MODE = 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')

As you can see in the above example, both recordsets store and use different fetch modes based on the $ADODB_FETCH_MODE setting when the recordset was created by Execute().

If no fetch mode is predefined, the fetch mode defaults to ADODB_FETCH_DEFAULT. The behaviour of this default mode varies from driver to driver, so do not rely on ADODB_FETCH_DEFAULT. For portability, we recommend sticking to ADODB_FETCH_NUM or ADODB_FETCH_ASSOC. Many drivers do not support ADODB_FETCH_BOTH.

SetFetchMode Function

Some programmers prefer to use a more object-oriented solution, where the fetch mode is set by a object function, SetFetchMode. Once this function is called for a connection object, that connection object will ignore the global variable $ADODB_FETCH_MODE and will use the internal fetchMode property exclusively.

    $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')

To retrieve the previous fetch mode, you can use check the $db->fetchMode property, or use the return value of SetFetchMode( ).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值