TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Export table to sql (http://www.talkphp.com/general/5425-export-table-sql.html)

Peuplarchie 05-25-2010 02:33 AM

Export table to sql
 
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.

PHP Code:

<?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!

Village Idiot 05-25-2010 06:23 AM

A couple minutes on google found this: http://davidwalsh.name/backup-mysql-database-php. It's only does the basics, but it should not be too hard to modify it to fit your needs.


All times are GMT. The time now is 06:12 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0