note 102114 deleted from function.mysql-field-flags by danbrown
| From: | danbrown@php.net | Date: | Thu, 27 Jan 2011 15:51:43 +0000 |
| Subject: | note 102114 deleted from function.mysql-field-flags by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176396@lists.php.net to get a copy of this message | ||
Note Submitter: gechillter
----
This function returns a phpmyadmin mysql dump.
Thanks to play & boma!
<?PHP
/**
/* backups either all tables or just one table from a database
/* Returns a phpmyadmin look alike mysql export
/**/
function mysql_backup($databaseName,$tableName=''){
$str_backup='';
//Get Auto_increment Values
$tab_status = mysql_query("SHOW TABLE STATUS");
while($all = mysql_fetch_assoc($tab_status)):
$tbl_stat[$all[Name]] = $all[Auto_increment];
endwhile;
$tables=array();
if($tableName==''){
//all tables in database
$tableinfos = mysql_list_tables($databaseName);
while($tab = mysql_fetch_row($tableinfos)){
array_push($tables,$tab[0]);
}
}else{
//just one table
array_push($tables,$tableName);
}
foreach ($tables as $table){
$str_backup .= "--\n-- table structure for
$table\n--\n\nCREATE TABLE IF NOT
EXISTS $table ( ";
$res = mysql_query("SHOW CREATE TABLE $table");
while($all = mysql_fetch_assoc($res)):
$str = str_replace("CREATE TABLE $table (", "",
$all['Create Table']);
$str = str_replace(",", ", ", $str);
$str = str_replace(") ) TYPE=MyISAM ", ")\n ) TYPE=MyISAM ",
$str);
$str_backup .= $str." AUTO_INCREMENT=".$tbl_stat[$table].";\n\n";
endwhile;
$str_backup .= "--\n-- data for table $table\n--\n\n";
$data = mysql_query("SELECT * FROM $table");
$nbc = mysql_num_fields($data);
while ($dt = mysql_fetch_array($data)){
$str_backup .= "INSERT INTO $table VALUES(";
for ($i=0; $i<$nbc; $i++){
if($i>0){$str_backup .= ", ";}
if(!isset($dt[$i])){
$str_backup .= "NULL";
}else{
$str_backup .= "'" . mysql_real_escape_string($dt[$i]). "'";
}
}
$str_backup .= ");\n";
}
$str_backup .= "\n-- --------------------------------------------------------\n\n";
}
return $str_backup;
}
?>