Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.
Quick MySQL Database Backup Script - PHP Code Bank
Quick MySQL Database Backup Script
Generates valid MySQL statements to recreate every database, table, and record in the MySQL database. If you find a more efficient way, comment!
<?php
// make database connection here / put db connect include here
// if you use a select_db statement, comment out the first query loop ("SHOW DATABASES" and the fetch_array)
// set this variable to 1 to include information_schema table in backup
$grabSchema = 0;
if ($a = mysql_query("SHOW DATABASES")) { while ($b = mysql_fetch_array($a)) { // grab database names
if ((!$grabSchema && $b[0] != 'information_schema') || $grabSchema) { // are we including information_schema?
echo "<p>CREATE DATABASE ".$b[0].";</p>";
mysql_select_db($b[0]); // let's select the db so we can start getting the tables
if ($c = mysql_query("SHOW TABLES")) { while ($d = mysql_fetch_array($c)) { // grab table names
$fields = array(); // need to store the field names for the inserts
$types = array(); // need to store the field types to know if we quote our inserts
$st = "<p>CREATE TABLE ".$d[0].'(';
$st2 = ''; // this will store the field information for our CREATE TABLE statement
if ($e = mysql_query("DESCRIBE ".$d[0])) { while ($f = mysql_fetch_array($e)) { // grab field names for table
$fields[] = $f['Field']; // add to field list for table
$types[] = $f['Type']; // add to type list for table
// Re-create the column creation statement using the attributes returned from "DESCRIBE"
$st2 .= $f['Field'].' '.$f['Type'].($f['Null'] == 'NO' ? ' NOT' : '').' NULL'.
(!empty($r['Extra']) ? ' '.$r['Extra'] : '');
if ($f['Key'] == 'PRI') { $st2 .= ' PRIMARY KEY'; }
if (!empty($f['Default'])) {
$st2 .= ' DEFAULT '.(strstr($f['Type'],'text') ? "'{$f['Default']}'" : $f['Default']);
}
// END re-create, add a comma for the next field
$st2 .= ',';
} } // Close "DESCRIBE tablename"
// If there are no fields left, finish out the CREATE TABLE statement
// If there were no fields in the table, leave out the parenthesis to make a valid CREATE TABLE statement
echo $st.(!empty($st2) ? substr($st2,0,strlen($st2) - 1).')' : '').';</p>';
// Now, we grab the contents of the table
if ($e = mysql_query("SELECT * FROM ".$d[0])) { while ($f = mysql_fetch_array($e)) {
$st = "<p>INSERT INTO ".$d[0]." SET ";
$count = count($f);
// Need to reference the field and type arrays by numeric index, so we put the count() outside of
// the for() loop for performance. Now, we loop through each field in the current row.
for($x = 0;$x < $count;$x++) {
// If the field isn't empty, insert either a string or non-string according to the type.
if (!empty($f[$x])) {
$st .= $fields[$x]."=".(strstr($types[$x],'text') || strstr($types[$x],'char') ? "'{$f[$x]}'" : $f[$x]).",";
}
}
// Finish out the insert by removing the last comma.
echo $st=substr($st,0,strlen($st) - 1).";</p>";
} } // Close "SELECT * FROM tablename"
} } // Close "SHOW TABLES"
} // Close conditional for $showSchema
} } // Close "SHOW DATABASES"
?>
Comments