5

When retrieving data from MySQL, the PHP array output has both numeric and name indexes. How can I prevent this?

I fetch using:

$stmt = $this->dbObj->Prepare($sql);
$rs = $this->dbObj->Execute($stmt);
if (!$rs) {
    trigger_error($this->dbObj->ErrorNo() . ' ' . $this->dbObj->ErrorMsg(), E_USER_ERROR);
}

$this->videos_voted = null;
while ($row = $rs->FetchRow()) {
    $this->videos_voted[$row['video_id']] = $row;
}

The output:

Array
(
    [16] => Array
        (
            [0] => 16
            [video_id] => 16
            [1] => 1028
            [total_views] => 1028
            [2] => No
            [featured] => No
        )

)

As you can see, the output has both numeric and name indexes.

Sascha Heim
  • 301
  • 1
  • 4
  • 15
  • i don't know what $rs represents, but it's because your FetchRow function is performing a mysql_fetch_array. You want it to perform a mysql_fetch_assoc. See if there any other row fetching functions available from $rs – Julien Nov 29 '11 at 19:54
  • @Julien - I'd say `$rs` represents an [ADORecordSet](http://phplens.com/adodb/reference.functions.adorecordset.html) object. – Álvaro González Nov 29 '11 at 19:58

2 Answers2

7

From: http://phplens.com/lens/adodb/docs-adodb.htm#adodb_fetch_mode

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

$db->SetFetchMode(ADODB_FETCH_ASSOC);
Mike B
  • 31,886
  • 13
  • 87
  • 111
2

Call SetFetchMode() to tell ADO what kind of array you want to get.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360