I am trying to export a MySQL database using PHP, I want the data to be saved to the users computer, I want this data to be saved as a .sql file. I am trying to make it as simple as phpmyadmin.
I have tried everything I can find on Google.
I have tried to do mysql dumps, custom scripts, but they all write the data to the server, never the client computer.
All help is appreciated!
You need to use header()
to set an appropriate content type and output the data.
Normal output is sent to the user as Content-Type: text/html
If you want to send output as a text document, use:
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="yourfilename.sql"');
echo $your_sql_content;
<?php
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=YOUR_EXPORT.sql");
header("Pragma: no-cache");
header("Expires: 0");
$fp = fopen('php://output', 'w');
$your_query = mysql_query( "select * from bla bla bla ... ");
while( $codes = mysql_fetch_array( $your_query ) ) {
$row = array();
$row[] = some data ...
fputs($fp, $row);
}
fclose($fp);