Mysql数据库有多种的备份方法,下面为您介绍的方法是根据Mysql表备份,该方法供您参考,希望对您学习Mysql表备份数据库方面能有所帮助。
- <?php
- function datatosql($table)
- {
- global $db;
- $tabledump = "DROP TABLE IF EXISTS $table;\n";
- $createtable = $db->query("SHOW CREATE TABLE $table");
- $create = $db->fetch_array($createtable);
- $tabledump .= $create[1].";\n\n";
- $rows = $db->query("SELECT * FROM $table");
- $numfields = $db->num_fields($rows);
- $numrows = $db->num_rows($rows);
- while ($row = $db->fetch_array($rows)){
- $comma = "";
- $tabledump .= "INSERT INTO $table VALUES(";
- for($i = 0; $i < $numfields; $i++)
- {
- $tabledump .= $comma."'".mysql_escape_string($row[$i])."'";
- $comma = ",";
- }
- $tabledump .= ");\n";
- }
- $tabledump .= "\n";
- return $tabledump;
- }
- ?>