Page 1 of 1
How to get a list of column names from sql query
Posted: Wed Jan 21, 2004 3:14 am
by Azhrarn
Heya,
I know its a stupid question, but how would I list out all the column names I got from an sql query?
I need for instance
<name of column> <value of column>
ie
computer_name computer-1
computer_name computer-2
computer_name computer-3
Thanks!
Azh
Posted: Wed Jan 21, 2004 3:33 am
by jolan_lars
if your using mysql, u can use:
mysql_field_name( resource result, field index);
eg.
$res = mysql_query("select computer_name from computers");
echo mysql_field_name($res, 0); //prints column name
$field=mysql_fetch_row($res);
echo $field[0];//prints column value
Posted: Wed Jan 21, 2004 3:34 am
by twigletmac
Assuming that you used MySQL, if you've returned the result as an associative array (using [php_man]mysql_fetch_assoc[/php_man]()), you could do:
Code: Select all
while ($row = mysql_fetch_assoc($result)) {
foreach ($row as $column_name => $column_value) {
echo $column_name.' '.$column_value.'<br />';
}
}
That's one way.
Mac
Posted: Wed Jan 21, 2004 3:45 am
by Azhrarn
thanks guys!
Posted: Wed Jan 21, 2004 2:48 pm
by pickle
Look here. It's almost exactly what you're asking for (I think).
http://ca.php.net/manual/en/function.my ... fields.php