Forum Moderators: coopster

Message Too Old, No Replies

Creation of CSV File

Outputting Column Names

         

paseo

10:40 pm on Feb 8, 2007 (gmt 0)

10+ Year Member



Hi,

I am currently able to export successfully the contents of a query into a Comma Delimited text file using the code below. WHat i need is for the the code to also export the Column Names so that they appear in the first row of the file. I was researching but was unable to come up with a concrete solution. Can anybody point me in the right direction? Something to do with mysql_field_name but im not quite sure how to integrate that with the code below...

<?
// connect to database
$result=mysql_query("select id
from tablename
where shid=3");
list($DBshid)=mysql_fetch_row($result);

/***********************************
Write Date to CSV file
***********************************/
$_file = 'show.csv';
$_fp = @fopen( $_file, 'w' );

$result=mysql_query("select name,compname,job_title,email_add,phone,url
from tablename
where id=3");
while (list($DBname,$DBcompname,$DBjob_title,$DBemail_add,$DBphone,$DBurl)=mysql_fetch_row($result))
{
$_csv_data=$DBname.','.$DBcompname.','.$DBjob_title.','.$DBemail_add.','.$DBphone.','.$DBurl . "\n";
@fwrite( $_fp, $_csv_data );
}
@fclose( $_fp );

?>

dreamcatcher

10:59 pm on Feb 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi paseo,


$columns = array();

$result=mysql_query("select COLUMNS from tablename") or die(mysql_error());

for ($i=0; $i<mysql_num_rows($result); $i++)
{
$row = mysql_fetch_row($result);
$columns[] = $row[0];
}

if (!empty($columns))
{
echo implode(",", $columns);
}

dc

paseo

11:00 pm on Feb 8, 2007 (gmt 0)

10+ Year Member



THanks! I will try it out and post results!

mcibor

11:07 pm on Feb 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, you can use mysql_field_name [pl.php.net], but if you have static table, then I would write the data manually, and moreover would paste the file write to the end:

$_csv_data = "Name, Company name, Job title, Email, Phone, Url\n";

$result=mysql_query("SELECT name,compname,job_title,email_add,phone,url
FROM tablename
WHERE id=3");
while (list($DBname,$DBcompname,$DBjob_title,$DBemail_add,$DBphone,$DBurl)=mysql_fetch_row($result))
{
$_csv_data .= "$DBname,$DBcompname,$DBjob_title,$DBemail_add,$DBphone,$DBurl\n";
}
$_file = 'show.csv';
$_fp = @fopen( $_file, 'w' );
@fwrite( $_fp, $_csv_data );
@fclose( $_fp );

?>
And voile, you've got yourself a csv with headers :)

Regards
Michal

or before while use:

$result=mysql_query("SELECT name,compname,job_title,email_add,phone,url
FROM tablename
WHERE id=3");
$_csv_data = mysql_field_name($result, 0).','.mysql_field_name($result, 1).','.mysql_field_name($result, 2)...
while (list($DBname,$DBcompname,$DBjob_title,$DBemail_add,$DBphone,$DBurl)=mysql_fetch_row($result))
{

paseo

11:24 pm on Feb 8, 2007 (gmt 0)

10+ Year Member



dc, mcibor

WORKED LIKE A CHAMP! i ended up using mcibor response just because i wasnt sure how to incorperate that into the exisiting code

One more question though...

How would i go about naming the file using the value from $DBfirstname...

I tried something like this but its not working

$_file = '$DBname.csv';

paseo

4:47 pm on Feb 9, 2007 (gmt 0)

10+ Year Member



Question for you guys...

If i had a string like

$_file = 'show.csv';

how would i format it so the show.csv will be the value of another variable///ex/

$_file = '$filename'

I could go like this $_file = $DBlastname; but the problem im having is attaching the .csv extion with keeping syntaxk ok.

dreamcatcher

5:39 pm on Feb 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$_file = $DBlastname.'.csv';

dc

paseo

5:46 pm on Feb 9, 2007 (gmt 0)

10+ Year Member



DC, u da man. I tried every which combination but that....

mcibor

4:11 pm on Feb 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also using double quotes would help:

$_file = "$DBname.csv";

Try to find out the difference between double and single quotes:

echo '$DBname.csv';
echo "DBname.csv";

There is much difference, I will tell you.

regards
Michal