Connecting Tech Pros Worldwide Forums | Help | Site Map

PHP/mySQL, get column headers?

Mick White
Guest
 
Posts: n/a
#1: Jul 17 '05
IS it possible in PHP to grab the headers following a mySQL query?

SELECT [headings] from tablename.

I'm looking for a generic PHP function that will include the headings,
so that I can display a meaningful table.

Mick

Chris Hope
Guest
 
Posts: n/a
#2: Jul 17 '05

re: PHP/mySQL, get column headers?


Mick White wrote:
[color=blue]
> IS it possible in PHP to grab the headers following a mySQL query?
>
> SELECT [headings] from tablename.
>
> I'm looking for a generic PHP function that will include the headings,
> so that I can display a meaningful table.[/color]

If you use mysql_fetch_assoc() or mysql_fetch_array() with MYSQL_ASSOC
as the 2nd parameter then you'll get an array indexed with the column
names.

For example:

$res = mysql_query('select a, b, c from foo');
$row = mysql_fetch_assoc($res);
print '<table><tr>';
foreach($row as $name => $value) {
print "<th>$name</th>";
}
print '</tr>';
while($row) {
print '<tr>';
foreach($row as $value) {
print "<td>$value</td>";
}
print '</tr>';
$row = mysql_fetch_assoc($res);
}
print '</table>';

http://www.php.net/mysql_fetch_assoc
http://www.php.net/mysql_fetch_array

You can also use the mysql_fetch_field() and mysql_field_name()
functions.

for($i = 0; $i < mysql_num_fields($res); $i++) {
print '<th>' . mysql_field_name($res, $i) . '</th>';
}

http://www.php.net/mysql_fetch_field
http://www.php.net/mysql_field_name
http://www.php.net/mysql_num_fields(

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jan Pieter Kunst
Guest
 
Posts: n/a
#3: Jul 17 '05

re: PHP/mySQL, get column headers?


Mick White wrote:[color=blue]
> IS it possible in PHP to grab the headers following a mySQL query?
>
> SELECT [headings] from tablename.
>
> I'm looking for a generic PHP function that will include the headings,
> so that I can display a meaningful table.
>
> Mick[/color]

<http://nl.php.net/manual/en/function.mysql-list-fields.php>

JP

--
Sorry, <devnull@cauce.org> is a spam trap.
Real e-mail address unavailable. 5000+ spams per month.
News Me
Guest
 
Posts: n/a
#4: Jul 17 '05

re: PHP/mySQL, get column headers?


Mick White wrote:[color=blue]
> IS it possible in PHP to grab the headers following a mySQL query?
>
> SELECT [headings] from tablename.
>
> I'm looking for a generic PHP function that will include the headings,
> so that I can display a meaningful table.
>
> Mick[/color]

DESCRIBE TABLE <tablename>

NM
Mick White
Guest
 
Posts: n/a
#5: Jul 17 '05

re: PHP/mySQL, get column headers?


Chris Hope wrote:[color=blue]
> Mick White wrote:
>
>[color=green]
>>IS it possible in PHP to grab the headers following a mySQL query?[/color][/color]
[color=blue]
>
> If you use mysql_fetch_assoc() or mysql_fetch_array() with MYSQL_ASSOC
> as the 2nd parameter then you'll get an array indexed with the column
> names.
>
> For example:
>
> $res = mysql_query('select a, b, c from foo');
> $row = mysql_fetch_assoc($res);
> print '<table><tr>';
> foreach($row as $name => $value) {
> print "<th>$name</th>";
> }
> print '</tr>';
> while($row) {
> print '<tr>';
> foreach($row as $value) {
> print "<td>$value</td>";
> }
> print '</tr>';
> $row = mysql_fetch_assoc($res);
> }
> print '</table>';
>
> http://www.php.net/mysql_fetch_assoc
> http://www.php.net/mysql_fetch_array
>
> You can also use the mysql_fetch_field() and mysql_field_name()
> functions.
>
> for($i = 0; $i < mysql_num_fields($res); $i++) {
> print '<th>' . mysql_field_name($res, $i) . '</th>';
> }
>
> http://www.php.net/mysql_fetch_field
> http://www.php.net/mysql_field_name
> http://www.php.net/mysql_num_fields(
>[/color]

Thanks, Chris (and others), that did the trick.
Mick
Closed Thread