DB_common::setFetchMode()

DB_common::setFetchMode() – Sets the default fetch mode

Synopsis

void setFetchMode ( integer $fetchmode , string $object_class = stdClass )

Description

Sets the default fetch mode used by fetch*() and get*() methods.

Parameter

integer $fetchmode

DB_FETCHMODE_ORDERED, DB_FETCHMODE_ASSOC or DB_FETCHMODE_OBJECT.

See the Examples section, below, for more information.

string $object_class

This parameter is for use when $fetchmode is set to DB_FETCHMODE_OBJECT.

You can set this parameter to DB_row, which then causes the resulting data to populate a new instance of a DB_row object.

Return value

void - nothing is returned on success or a DB_Error object on failure

Throws

Possible PEAR_Error values
Error code Error message Reason Solution
NULL invalid fetchmode mode The given fetch mode does not exists or is not implement in your DB version. Check writing of the argument and your used version of DB.

Note

This function can not be called statically.

Example

DB_FETCHMODE_ORDERED (default)

Causes ordered arrays to be returned. The order is taken from the select statement.

<?php
// Once you have a valid DB object named $db...
$db->setFetchMode(DB_FETCHMODE_ORDERED);

$res =& $db->query('SELECT a, b FROM phptest WHERE a = 28');
$row =& $res->fetchRow();

print_r($row);
echo 
'Column a is ' $row[0];
?>

Output:

    
Array
(
    [0] => 28
    [1] => hi
)
Column a is 28
    

DB_FETCHMODE_ASSOC

Makes associative arrays, with the column names as the array keys.

<?php
// Once you have a valid DB object named $db...
$db->setFetchMode(DB_FETCHMODE_ASSOC);

$res =& $db->query('SELECT a, b FROM phptest WHERE a = 28');
$row =& $res->fetchRow();

print_r($row);
echo 
'Column a is ' $row['a'];
?>

Output:

    
Array
(
    [a] => 28
    [b] => hi
)
Column a is 28
      

DB_FETCHMODE_OBJECT

Returns objects with column names as properties.

<?php
// Once you have a valid DB object named $db...
$db->setFetchMode(DB_FETCHMODE_OBJECT);

$res =& $db->query('SELECT a, b FROM phptest WHERE a = 28');
$row =& $res->fetchRow();

print_r($row);
echo 
'Column a is ' $row->a;
?>

Output:

    
stdClass Object
(
    [a] => 28
    [b] => hi
)
Column a is 28
      

DB_FETCHMODE_OBJECT and DB_row

If setFetchMode()'s optional $object_class parameter is set to DB_row, DB_row objects are returned.

<?php
// Once you have a valid DB object named $db...
$db->setFetchMode(DB_FETCHMODE_OBJECT'DB_row');

$res =& $db->query('SELECT a, b FROM phptest WHERE a = 28');
$row =& $res->fetchRow();

print_r($row);
echo 
'Column a is ' $row->a;
?>

Output:

    
db_row Object
(
    [a] => 28
    [b] => hi
)
Column a is 28
    

DB_FETCHMODE_OBJECT with your own object in PHP 4

<?php
// Once you have a valid DB object named $db...

class SomeResult {
    function 
SomeResult($data) {
        foreach (
$data as $key => $value) {
            
$this->$key $data[$key];
        }
    }
}

$db->setFetchMode(DB_FETCHMODE_OBJECT'SomeResult');

$res =& $db->query('SELECT a, b FROM phptest WHERE a = 28');
$row =& $res->fetchRow();

print_r($row);
echo 
'Column a is ' $row->a;
?>

Output:

    
SomeResult Object
(
    [a] => 28
    [b] => hi
)
Column a is 28
    

DB_FETCHMODE_OBJECT with your own object in PHP 5

<?php
// Once you have a valid DB object named $db...

class SomeResult {
    public 
$row_data;
    function 
__construct($data) {
        
$this->row_data $data;
    }
    function 
__get($variable) {
        return 
$this->row_data[$variable];
    }
}

$db->setFetchMode(DB_FETCHMODE_OBJECT'SomeResult');

$res =& $db->query('SELECT a, b FROM phptest WHERE a = 28');
$row =& $res->fetchRow();

print_r($row);
echo 
'Column a is ' $row->a;
?>

Output:

    
SomeResult Object
(
    [a] => 28
    [b] => hi
)
Column a is 28