473,890 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 28947
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_ass oc() or mysql_fetch_arr ay() with MYSQL_ASSOC
as the 2nd parameter then you'll get an array indexed with the column
names.

For example:

$res = mysql_query('se lect a, b, c from foo');
$row = mysql_fetch_ass oc($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_ass oc($res);
}
print '</table>';

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

You can also use the mysql_fetch_fie ld() and mysql_field_nam e()
functions.

for($i = 0; $i < mysql_num_field s($res); $i++) {
print '<th>' . mysql_field_nam e($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_ass oc() or mysql_fetch_arr ay() with MYSQL_ASSOC
as the 2nd parameter then you'll get an array indexed with the column
names.

For example:

$res = mysql_query('se lect a, b, c from foo');
$row = mysql_fetch_ass oc($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_ass oc($res);
}
print '</table>';

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

You can also use the mysql_fetch_fie ld() and mysql_field_nam e()
functions.

for($i = 0; $i < mysql_num_field s($res); $i++) {
print '<th>' . mysql_field_nam e($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
9285
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 is there a better way? -- Bush crime family lost/embezzled $3 trillion from Pentagon. Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video. http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm
2
1874
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 position (hence, they do not need to be scrolled by the system when the AutoScrollPosition is changed), I do not want to draw them each time the control needs to be repainted or the user scrolls down. The only way I can think of to do this right...
2
433
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 item. ListViewItem item1 = new ListViewItem("item1",0); // Place a check mark next to the item.
2
9699
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. The dataset is created at run time, so I don't know column headers in advance. The table's column captions are set at run time, using the DataColumn.Caption property. The table is bound to the grid using the grid's DataSource property. Here is my...
4
5098
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. Thanks Hans
1
10246
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
4655
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: http://web.tampabay.rr.com/bmerkey/examples/locked-column-csv.html http://www.adp-gmbh.ch/web/css/expression.html
2
2002
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 headers scrolls up off the screen and it becomes hard for the user to determine what the columns are. Is there a way to keep the columns headers in place (only for vertical scrolling) when the user scrolls down. Thanks, John
0
1182
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 2.0, the column headers were not painted correctly when setting this property to false and resizing the control to make it larger. To work around this issue, set this property to true in a ResizeBegin event handler and set it back to false in a...
1
4674
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 headers so I thought I would post one that does. On the controls property simply click yes or no for Column Headers and off you go. You can use the Tag property to determine the column and the words displayed. 2;<No Selection> To help save a...
0
9819
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11222
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10918
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10460
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8015
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7169
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5845
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6041
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4270
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.