473,406 Members | 2,343 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,406 software developers and data experts.

Getting column names from MySQL query result

170 100+
I picked up the following code to take a mysql table to an excel file. What do I need to do to it to make the first excel row be the column names, rather than the first record?
Expand|Select|Wrap|Line Numbers
  1. $query  = "SELECT * FROM mytable";
  2. $result = mysql_query($query) or die('Error, query failed');
  3.  
  4. $tsv  = array();
  5.  
  6. while($row = mysql_fetch_array($result, MYSQL_NUM))
  7. {
  8.    $tsv[]  = implode("\t", $row);
  9. }
  10.  
  11. $tsv = implode("\r\n", $tsv);
  12.  
  13. $fileName = 'file.xls';
  14. header("Content-type: application/vnd.ms-excel");
  15. header("Content-Disposition: attachment; filename=$fileName");
  16.  
  17. echo $tsv;
  18.  
Mar 31 '07 #1
3 13568
ronverdonk
4,258 Expert 4TB
When you want to show the column names of your result, you have to use the mysql_field_name command. The following code snippet shows you how to display the column names of the result onmto the screen.

You can adapt it to your own choosing.

[php]
if (mysql_num_rows($result) > 0) {
$numfields = mysql_num_fields($result);
for ($i=0; $i < $numfields; $i++)
echo mysql_field_name($result, $i).'</br>';
}[/php]

Ronald :cool:
Mar 31 '07 #2
beary
170 100+
Thanks Ronald.

When you want to show the column names of your result, you have to use the mysql_field_name command. The following code snippet shows you how to display the column names of the result onmto the screen.

You can adapt it to your own choosing.

[php]
if (mysql_num_rows($result) > 0) {
$numfields = mysql_num_fields($result);
for ($i=0; $i < $numfields; $i++)
echo mysql_field_name($result, $i).'</br>';
}[/php]

Ronald :cool:
Apr 1 '07 #3
ronverdonk
4,258 Expert 4TB
You are welcome. See you next time.

Ronald :cool:
Apr 1 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: James | last post by:
Please help - getting very desperate! Sun, 12 October 2003 05:39 I have PHPDEV 4.2.3 from Firepages.com.au as the upgrade to 4.3.0 did not work. I also had an abortive download from PHP.NET as...
2
by: Wayne Pierce | last post by:
I have a small script with PHP that queries a MySQL database to pull out one row, where I want to be able to access each of the columns separately. I have tried several different variations and am...
2
by: kindermaxiz | last post by:
hi there i have a mysql table and i want the user to add columns using a php interface and then display the table with the newly added column. I was thinking about putting all the columns...
4
by: ChronoFish | last post by:
In PHP you can retrieve the table.column name of a MySQL query doing something like this: $result = mysql_query($query, $dbConnection); $resultArray = mysql_fetch_row($result); $i = 0;...
2
by: Joe Gazda | last post by:
I'm a relative newbie to PHP, but have been able to put together some PHP code to generate a CSV/XLS file from a Query result. Now, I would like to include custom column names instead of the MySQL...
11
by: Randell D. | last post by:
Folks, I have a table of addresses and a seperate table with contact names - All addresses tie to one or more names - I would like to keep track of the number of names 'belonging' to an address...
7
by: gemel | last post by:
I am developing an application that uses SQL 2000 as my source of images. I have successfully created the code to load the images onto the SQL Server and also to retrieve the images into a dataset....
4
by: rass.elma | last post by:
Hi all I fetch the following value from a string (VCAHR(250))colmun in a MySql table: "300000000000000000000000000000000000000000000000000" When I write it out using echo() I get : 3E+50 ...
3
by: koti688 | last post by:
how to get the column names and the values of them in select statment. I have a packaze like this named ADB.pm package ADB; use DBI; @ISA = ('Exporter');
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
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,...
0
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...
0
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,...
0
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...

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.