...

View Full Version : Back Up database as csv file



newphpcoder
01-27-2012, 02:37 AM
Hi


I search for a sample code for back up database as csv file.

I found this code:



<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'payroll';
$table = 'generate_payroll';
$file = 'export';
$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");
$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field']."; ";
$i++;
}
}
$csv_output .= "\n";
$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j]."; ";
}
$csv_output .= "\n";
}
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?>


when I run this code, theres a pop up window asking if i want to open or save, when i click open or save i got an error.

I got a pop up Window Internet Explorer

Saying:
Internet Explorer was not able to open this internet site. The requested site is either unavailable or cannot be found.Please try again.


Thank you

jmj001
01-27-2012, 02:39 AM
.. edit - removed --

sorry.. didn't read the question properly..

I can't see anything in there that is creating a file, it's just echoing to screen... but the header is telling the browser there's a file but there is actually no file...

KuriosJon
01-27-2012, 03:00 AM
it's just echoing to screen... but the header is telling the browser there's a file but there is actually no file...

Setting a proper header and then echoing content tells the browser to treat that content as a certain file type.

newphpcoder, comment out the three lines at the end starting with header and then go to the web page. If there are PHP errors, then there's a problem with your export script. If you actually see a proper CSV file in your browser, then there's an issue with the headers.

jmj001
01-27-2012, 03:03 AM
.. ..



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum