Forum Moderators: coopster

Message Too Old, No Replies

Generating a CSV file from MySQL Problem

         

indesign

12:00 am on Nov 4, 2005 (gmt 0)

10+ Year Member



I had been looking for some help about downloading a csv file form a mysql table using a form submit. This forum provided an excellent answer and I have given it a try with the following.

<?
if($submit) {
include "dbconnect.php";
$content_file = "";
$content_file .= "PurchaseID,Date,IssuedTo,Amount,Reviewed,EnteredBy,Comments,JobID\r\n";
$sql = 'select * from po';
$statement = mysql_query($sql);
while($row = mysql_fetch_array($statement)) {
$content_file .= $row['PurchaseID'] . ',' . $row['Date'] . ',' . $row['IssuedTo'] . ',' . $row['Amount'] . ',' . $row['Reviewed'] . ',' . $row['EnteredBy'] . ',' . $row['Comments'] . ',' . $row['JobID'] . "\r\n";
}
$output_file = 'purchaseorders.csv';
@ini_set('zlib.output_compression', 'Off');
header('Pragma: public');
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: pre-check=0, post-check=0, max-age=0');
header('Content-Transfer-Encoding: none');
//This should work for IE & Opera
header('Content-Type: application/octetstream; name="' . $output_file . '"');
//This should work for the rest
header('Content-Type: application/octet-stream; name="' . $output_file . '"');
header('Content-Disposition: inline; filename="' . $output_file . '"');
echo $content_file;
exit();
}
?>

<form name="form1" method="post" action="downloadpo.php">
<input type="submit" name="submit" value="Download">
</form>

My only problem is that when I save the csv I only get the field names from the database. It is very nice to get those, but the records would be a definite bonus.

I am so grateful for the help I have found here. Can anyone possibly see what I have done wrong?

Thanks so much for your help.

sned

12:14 am on Nov 4, 2005 (gmt 0)

10+ Year Member



include "dbconnect.php";
$content_file = "";
...
$sql = 'select * from po';

What is in your dbconnect.php file? I don't see where you make the actual db_connect() call, unless it's in the included file. That might be the first place to look.

-sned

indesign

1:39 am on Nov 4, 2005 (gmt 0)

10+ Year Member



It is in the dbconnect.php, but it was the problem. My only excuse for that stupidity is being so tired. It was in a folder that I didn't put in the path.

Sorry for the dumb post. Thanks alot for answering.

coopster

9:45 pm on Nov 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



No such thing as a dumb post, indesign. We all need a second set of eyes sometimes. Glad you got it sorted and welcome to WebmasterWorld ;)

indesign

1:08 pm on Nov 7, 2005 (gmt 0)

10+ Year Member



Thanks so much. I have found a wealth of information here. It is a great place.