473,385 Members | 1,824 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 28911
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Roedy Green | last post by:
In a spreadsheet you can arrange that column headers are always visible no matter how much your scroll the table itself. What is the best way to get this effect in CSS?. Does it require frames or...
2
by: Yaron | last post by:
Hi I'm creating a control very similar to the ListView in Details mode. I'm confused as to how to go about drawing the column headers at the top As the column headers are static and stay in one...
2
by: MFRASER | last post by:
I am trying to populate a listview and I can not see the column headers, is there a property I am forgetting to set? Here is my code // Create three items and three sets of subitems for each...
2
by: David Veeneman | last post by:
How can I set a bound DataGridView control to use a dataset table's column captions, instead of column names? I'm working with a DataGridView control, which I have bound to a table in a dataset....
4
by: Hans Merkl | last post by:
Hi, Is there a way to show the column headers of a GridView control even if there is no data? The only thing I see is the EmptyDataTemplate but I would also like to display the column headers. ...
1
by: ElenaR | last post by:
I need to figure out how to name my column headers in a DataGrid. In VB6, I could write DataGrid1.Columns(0).Caption = "ID". What is the format for VB.NET? Thanks in Advance!
0
by: nkparimi | last post by:
Hi, Here's what I'm trying: given an html table, to freeze the column headers and/or left column in IE. I understand that this is possible thru style sheets in IE, as suggested in the following:...
2
by: John Walker | last post by:
Hi, We have a datagrid with column headers. If the datagrid has more than say 25 rows the user needs to scroll down to be able to see the rest of the grid. When the user does this the column...
0
by: erik.alsmyr | last post by:
When setting the Scrollable property of a listview and resizeing it the headers will be painted wrongly. This defect is documented in MSDN as: In versions of the .NET Framework prior to version...
1
Denburt
by: Denburt | last post by:
This function was originally created back in the dawn of time, over the years I have made a number of changes and after searching the net I still didn't see 1 that allowed for the use of column...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.