see newer version below
...
$today = strftime('%Y%m%d');
$backupName = "backup-$today.sql";
if (! ($backupFile = fopen($backupName, 'w'))) {
exit(1);
}
chmod($backupName, 0600);
mysql_select_db('change me!'); // ******Change this to your database name
...
fwrite($backupFile, "INSERT INTO $row[0] VALUES ";
if ($tableRow = mysql_fetch_row()) {
// write the first row; no leading comma
$values = implode("', '", array_map('mysql_escape_string', $tableRow));
fwrite($backupFile, "('$values')" );
}
while($tableRow = mysql_fetch_row()) { // loop through all table rows
// write subsequent rows, which have leading commas
$values = implode("', '", array_map('mysql_escape_string', $tableRow));
fwrite($backupFile, ", ('$values')" );
}
fwrite(";\n");
}
fclose($backupFile);
<?php
function exportMysql($fileName, $databaseName) {
mysql_select_db('INFORMATION_SCHEMA');
$file = ''; // this will hold the information
$query = "SELECT TABLE_NAME FROM TABLES WHERE TABLE_SCHEMA = '{$databaseName}' AND TABLE_TYPE = 'BASE TABLE'"; // fetch all table names
$tableNames = mysql_query($query); // query DB
if (!$tableNames) { // if there's an error
exit(mysql_error());
}
mysql_select_db($databaseName); // select database to back up
while($row = mysql_fetch_row($tableNames)) { // loop through all tables
$query = "SELECT * FROM {$row[0]}"; // select everything in the table
$tableData = mysql_query($query); //query DB
if (!$tableData) { // if there's an error
exit(mysql_error());
}
while($tableRow = mysql_fetch_row($tableData)) { // loop through all table rows
$data = implode("', '", $tableRow);
$data = "'{$data}'";
$file .= "INSERT INTO {$row[0]} VALUES({$data})\n";
}
}
file_put_contents($fileName, $file); // save file
}
function importMysql($fileName, $databaseName) {
if (!file_exists($fileName)) {
exit("File {$fileName} does not exist");
}
$file = file_get_contents($fileName);
$queries = explode("\n", $file);
mysql_select_db($databaseName);
foreach($queries as $query) {
mysql_query($query);
}
}
?>
$values = implode("', '", array_map('mysql_escape_string', $tableRow));
function exportMysql($fileName, $databaseName) {
mysql_select_db('INFORMATION_SCHEMA');
$file = ''; // this will hold the information
$query = "SELECT TABLE_NAME FROM TABLES WHERE TABLE_SCHEMA = '{$databaseName}' AND TABLE_TYPE = 'BASE TABLE'"; // fetch all table names
echo $query."<br>"; //I added this
$tableNames = mysql_query($query); // query DB
if (!$tableNames) { // if there's an error
exit(mysql_error());
}
mysql_select_db($databaseName); // select database to back up
while($row = mysql_fetch_row($tableNames)) { // loop through all tables
$query = "SELECT * FROM {$row[0]}"; // select everything in the table
$tableData = mysql_query($query); //query DB
if (!$tableData) { // if there's an error
exit(mysql_error());
}
while($tableRow = mysql_fetch_row($tableData)) { // loop through all table rows
$data = implode("', '", $tableRow);
$data = "'{$data}'";
$file .= "INSERT INTO {$row[0]} VALUES({$data})\n";
}
}
echo $file;// I added this as well.
file_put_contents($fileName, $file); // save file
}
include 'accessdb.php';//my php file that I use to log into my database using an account with full acccess
exportMysql(backup_db.sql,robrow_db);
SELECT TABLE_NAME FROM TABLES WHERE TABLE_SCHEMA = 'robrow_db' AND TABLE_TYPE = 'BASE TABLE'
INSERT INTO arms VALUES('10', 'Pinkett', '2600', '13890', '480', '80', '110', '30', '10', 'Standard', '0', '0', '0') INSERT INTO arms VALUES('11', 'Mekhi', '2486', '13298', '450', '77', '102', '39', '14', 'Light', '40000', '0', '0') INSERT INTO arms VALUES('12', 'Frontliner', '3430', '15296', '481', '102', '155', '26', '7', 'Standard', '40000', '0', '0') INSERT INTO arms VALUES('13', 'Rushnik', '2702', '13295', '466', '83', '110', '28', '19', 'Light', '45000', '0', '0')
...etc etc...goes on for awhile...
You can use 'IF NOT EXISTS' to check if the table exists and abort the import if it does.I'll try to work on this additional functionality in the morning. Also, when you restore the table, it won't check for duplicates, so you should DROP the table first, then create a new one from the definition.