472,143 Members | 1,698 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,143 software developers and data experts.

PHP/mySQL, get column headers?

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
Jul 17 '05 #1
4 28772
Mick White wrote:
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.


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/
Jul 17 '05 #2
Mick White wrote:
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


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

JP

--
Sorry, <de*****@cauce.org> is a spam trap.
Real e-mail address unavailable. 5000+ spams per month.
Jul 17 '05 #3
Mick White wrote:
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


DESCRIBE TABLE <tablename>

NM
Jul 17 '05 #4
Chris Hope wrote:
Mick White wrote:

IS it possible in PHP to grab the headers following a mySQL query?

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(


Thanks, Chris (and others), that did the trick.
Mick
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Roedy Green | last post: by
2 posts views Thread by Yaron | last post: by
2 posts views Thread by MFRASER | last post: by
2 posts views Thread by David Veeneman | last post: by
1 post views Thread by ElenaR | last post: by
reply views Thread by nkparimi | last post: by
2 posts views Thread by John Walker | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.