473,588 Members | 2,512 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 28927
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
9264
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
1854
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
9662
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
5086
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
10228
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
4645
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
1986
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
1161
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
4653
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
7929
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7860
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
8354
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7984
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
8223
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...
0
6634
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5726
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
3847
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
3883
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.