Page 1 of 1

Export table to sql

Posted: Mon May 24, 2010 9:30 pm
by Peuplarchie
Good day to you,
I have added an options to my db production site which is to back up the table into csv file, see code below.

Now I would like to be able to export to sql file, how would I do it without using phpMyadmin , I would like to use a function like the following.

Code: Select all

<?php
$host = 'localhost';
$user = '....';
$pass = '....';
$db = '....';
$table = $_GET[table];
$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 = "Backup__".$table."__".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;
echo "Exporting...";
?>

Thanks!

Re: Export table to sql

Posted: Mon May 24, 2010 9:38 pm
by Jonah Bron
A quick google brings some excellent results

http://www.google.com/search?q=php+expo ... l+sql+file

Re: Export table to sql

Posted: Mon May 24, 2010 9:43 pm
by Peuplarchie
I always "stfw" before posting a request if I came here it is because I didn't found what I was looking for after a few ours of serach,
I need a script that I can trigger from a link that would bring up the sql file to download.

Re: Export table to sql

Posted: Mon May 24, 2010 9:48 pm
by mikosiko
Peuplarchie wrote: Now I would like to be able to export to sql file,
Could you please clarify what exactly do you mean by "export to a sql file" ?

Re: Export table to sql

Posted: Mon May 24, 2010 10:02 pm
by Peuplarchie
I would like to take my mysql table and download it as an sql file

Re: Export table to sql

Posted: Mon May 24, 2010 10:08 pm
by mikosiko
Peuplarchie wrote:I would like to take my mysql table and download it as an sql file
do you mean the table structure?

if so... read the sentence SHOW CREATE TABLE ....
http://dev.mysql.com/doc/refman/5.0/en/ ... table.html

Re: Export table to sql

Posted: Mon May 24, 2010 10:12 pm
by Peuplarchie
structure and data

Re: Export table to sql

Posted: Mon May 24, 2010 10:13 pm
by Peuplarchie
I don't want to copy the table to another table.
I would like to backup the table structure and data and download into a .sql file.

Re: Export table to sql

Posted: Mon May 24, 2010 10:28 pm
by mikosiko
Peuplarchie wrote:structure and data
mysqldump

or if you want to do it programing you can use together
- SHOW CREATE TABLE sentence for the structure and format the result and this sentence to produce a csv file with the date
- SELECT * FROM your-table INTO OUTFILE 'your-file'
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n';

and yes... you can execute both from PHP... only limitation is in the case of the SELECT INTO OUTFILE sentence is that the file is created in the server

Re: Export table to sql

Posted: Mon May 24, 2010 11:01 pm
by Jonah Bron
And change the Content-type header appropriately with the header() function.