tags:

views:

425

answers:

2

Hello all,

I am making use of Adodb and I wish to get the field names of a result set. How do I do this?

$result = &$db->Execute($query);

The above query has executed successfully, I just don't know how to get the field names from that one row and loop through it.

I am making use of an access database,

I couldn't find anything in the docs when I searched. Probably not using the right terms.

Thanks all

+1  A: 

It will depend on your fetch mode - if you setFetchMode to ADODB_FETCH_NUM (probably the default) each row contains a flat array of columns. If you setFetchMode to ADODB_FETCH_ASSOC you get an associative array where you can access each value by a key. The following is taken from ADODB documentation - http://phplens.com/lens/adodb/docs-adodb.htm#ex1

$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 loop through a set of results:

$result = &$db->Execute($query);
foreach ($result as $row) {
    print_r($row);
}
thetaiko
Thank you for the explanation. :) It worked for me.
Abs
+1  A: 

You are looking for this i suppose:

$result = &$db->Execute($query);
$rows = $result->GetRows();
$fields = $result->fields;

// print rows
foreach($rows as $row)
{
   echo $row. '<br />';
}

// print fields
foreach($fields as $field)
{
   echo $field. '<br />';
}
Sarfraz