3

I have a table with a primary key called 'id' and I am using ADODB and getting a ADORecordSet_mysql back. I need an array where the id is associated with a row in the result set, but ADODBRecordSet_mysql only seems to have a GetArray(int startingRow) method that returns an array indexed by startingRow (where the default is 0).

I don't want to have to iterate through this result set an associate the id's to each row myself and I don't like the idea of having to pass into GetArray a starting index. I would rather be able to get the array back with it indexed using my primary key.

Is this possible or is my head in the clouds?

dregad
  • 1,150
  • 8
  • 21
stevebot
  • 23,275
  • 29
  • 119
  • 181

1 Answers1

0

The getAssoc() method will do exactly what you need:

  • When 2 columns are requested, the first column becomes the key and the second the value
  • When more than 2 columns are requested, the first column becomes the key, all other columns become a numeric array beneath that key.
// $db is connected ADOConnection object with 
print_r($db->getAssoc('select * from employee'));

/* 
Sample output with $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC
assuming employee table with columns id, name, salary

Array
(
    [100] => Array
        (
            [name] => Bob
            [salary] => 10000
        )
    [103] => Array
        (
            [name] => Sally
            [salary] => 12000
        )
*/

And if you need to have the id in the output as well, simply query SELECT id, e.* FROM employee e

dregad
  • 1,150
  • 8
  • 21