Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HELP! Need to download the Queried entry in Mysql

Status
Not open for further replies.

nelpalad

Technical User
Dec 7, 2004
7
US
Hi,

Im new to PHP, can anyone help how to download the queried data in MYsql to tab or comma delimited format file? I have no idea on this. Still searching the web for some useful tips.

Thanks
 
Apologies if this reply is a little too wordy. I had a similar requirement a while back and put this together. It works as you describe in multiple browsers and operating systems.

Code:
<?
// set up the database access
$my_serverName = "localhost";
$my_userName = "myprivateusername";
$my_password = "myprivatepassword";
$my_database = "mydatabasename";

// open a connection to my database
$link = @mysql_connect($my_serverName, $my_userName, $my_password);
mysql_select_db($my_database);

// perform the select from the table
$result = mysql_query("SELECT * FROM mytable");

// set up the CSV headers for the output string
$my_data = '"ID","Name","Phone number"';
$my_data .= "\n";

// loop through our dataset and append each row to the CSV output string
while ($row = mysql_fetch_array($result))
{
	$my_data .= $row["id"] . ',';
	$my_data .= '"' . $row["name"] . '",';
	$my_data .= '"' . $row["telephone"] . '",';
	$my_data .= "\n";
}

// close the database connection
mysql_close($link);

// define the output filename (should the user want to save the file)
$output_file = 'MyExportedData.csv';

// turn off zlib compression
if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off');

// send all these headers to the browser first
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');
header('Content-Type: application/octetstream; name="' . $output_file . '"');
header('Content-Type: application/octet-stream; name="' . $output_file . '"');
header('Content-Disposition: attachment; filename="' . $output_file . '"');

// send the CSV data string to the browser
echo $my_data;

// done
exit();
?>

Cheers,
Jeff
 
Hi Jeff,

Thanks, i will try this at home... I'll email if it meets my requirements.. thanks again
 
if you want to write directly to a file look at fopen and fwrite on the php.net website.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top