ADOdb

Database Abstraction Layer for PHP

User Tools

Site Tools


v5:reference:connection:setfetchmode

setFetchMode

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.

See Also

$ADODB_FETCH_MODE
getFetchMode

Syntax
  int setFetchMode(
  optional int $fetchMode
  )

Use as a getter

Use

setFetchMode()

without an argument to return the current fetch mode


Constants

Name Value Description
ADODB_FETCH_DEFAULT 0 The recordset is returned in the default provided by the PHP driver. Use of this value is not recommended if writing cross-database applications
ADODB_FETCH_NUM 1 The recordset is returned as a numeric array
ADODB_FETCH_ASSOC 2 The recordset is returned as an associative array
ADODB_FETCH_BOTH 3 The record is returned as both a numeric and associative arrays. This option is not supported by all databases

Usage

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

Fetch Both

Some databases support a 'Fetch Both' mode, which can be set using ADODB_FETCH_MODE. In this mode, the recordset contains data as both a numeric and an associative array, interspersed.

$db->setFetchMode(ADODB_FETCH_BOTH)
$rs3 = $db->execute('select * from table'); 
print_r($rs3->fields); 
/*
  shows _array([0]=>'v0',
               ['col1']=>'v0',
               [1] =>'v1',
               ['col2']=>'v1'
               )
*/
v5/reference/connection/setfetchmode.txt · Last modified: 2018/06/11 10:22 by dregad