Hi,
This is a php script I made because I was curious how the automated backup of the mysql databases was achieved.
PHP Code:
<?php
//Name of host you want to use
$db_host = "localhost";
//Name of database you want to use
$db_name = "students";
//User
$db_user = "root";
//Password
$db_pass = "";
//Name of table or tables
$table = "students";
mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db_name) or die("Unable to select database.");
function datadump ($table) {
$result ="";
$row="";
$result .= "# Dump of $table \n";
$result .= "# Dump DATE : " . date("d-M-Y") ."\n\n";
$query = mysql_query("select * from $table");
$num_fields = @mysql_num_fields($query);
//Retrieve the data from the original database
for ($i =0; $i <$num_fields; $i++) {
$result .= "INSERT INTO ".$table." VALUES(";
$data = mysql_query("SELECT * FROM $table");
while($info = mysql_fetch_array( $data,MYSQL_ASSOC )){
$comma_separated = implode(",", $info);
//Insert the data from the original array in to sql statement
if ($i<($num_fields)) $result .= "$comma_separated";
$result .= ");\n";
}
return $result . "\n\n\n";
}
}
$table1 = datadump ($table); //Add more datadumps if needed
$content = $table1; // Add additional datadumps to the string
//Creates an .sql file for download to your computer
$file_name = "MySQL_Database_Backup.sql";
Header("Content-type: application/octet-stream");
Header("Content-Disposition: attachment; filename=$file_name");
echo $content;
exit;
?>
If you have any comments or suggestions on how I can improve the code please post them. By the way, I'm hoping to create a totally automated back script sometime.
Edit:
Forgot to mention the php code version which is 5.3.0.
Nightwalker