ADOdb Library for PHP Manual
Prev $ADODB_FETCH_MODE Next

$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

If you have multiple connection objects, and want to have different fetch modes for each connection, then use 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( ).


Prev Home Next
$ADODB_LANG Up ADODB_ASSOC_CASE

Sponsored by phpLens