用PHP程序导出备份MYSQL的sql文件

'; 
	echo '===================================================
'; $time = explode(' ', microtime()); $start_time = $time[1] + $time[0]; //打开或创建一个用来存放备份数据库代码的文件 $fo = @fopen($save_path.$database_name.'.sql', 'a'); if($fo === false){ echo '文件找不到,或您没有权限打开该文件!'; } //连接数据库 $link = @mysql_connect('localhost', 'root', 'root') or die('数据库连接失败,error:'.mysql_error()); //获取该数据库中的表 $ret = mysql_list_tables($database_name); $table_list = array(); while($table = mysql_fetch_assoc($ret)){ $table_list[] = $table['Tables_in_'.$database_name]; echo ''.$table['Tables_in_'.$database_name].'
'; } foreach($table_list as $table){ //获取当前表被创建时的sql语句 $sql = 'show create table '.$table; $create_table_ret = mysql_fetch_assoc(mysql_query($sql)); $create_table_sql = 'drop table if exists '.$table.";\n"; $create_table_sql .= $create_table_ret['Create Table'].";\n"; //获取当前表中的数据并组成插入形式的sql语句 $sql = 'select * from '.$table; $insert_data_ret = mysql_query($sql); $insert_data_sql = 'insert into '.$table.' values'; while($row = mysql_fetch_assoc($insert_data_ret)){ $insert_data_sql .= '('; foreach($row as $key => $val){ $insert_data_sql .= '"'.mysql_real_escape_string($val, $link).'",'; } $insert_data_sql = rtrim($insert_data_sql, ',').'),'; } $insert_data_sql = rtrim($insert_data_sql, ',').";\n\n\n"; if(!mysql_num_rows($insert_data_ret)){ $create_table_sql .= "\n\n"; } fwrite($fo, $create_table_sql); if(mysql_num_rows($insert_data_ret)){ fwrite($fo, $insert_data_sql); } $innerHTML = $table."[备份完成!]"; $script = ''; echo $script; } fclose($fo); $time = explode(' ', microtime()); $end_time = $time[1] + $time[0]; echo '备份完成(共计'.count($table_list).'个表),花费时间为:', $end_time - $start_time, '秒!'; } dump_database('mysql', './');


你可能感兴趣的:(20),其他技术相关)