0

当数据输入到我的 web 表单中时,我使用 mysql_real_escape_string() 来逃避坏东西并使用 PHP 将其存储在 MySQL 中。我现在设置了一个脚本来创建一个备份 SQL 文件,以防我必须重新加载数据。

问题:插入语句中的“...Joe\\'s trucking...”之类的额外字符串导致我下面的代码失败。有没有更好的方法来自动备份和恢复 mysql?或者也许我该如何修复导致恢复失败的数据?

$mysqli_link = mysqli_connect("localhost","xxxx","xxxxxx","xxxxxxx");

/* check connection */
if (mysqli_connect_errno()) {
   printf("Connect failed: %s\n", mysqli_connect_error());
   exit();
}

$query =".......
LOCK TABLES `tbl_serv_prov` WRITE;
/*!40000 ALTER TABLE `tbl_serv_prov` DISABLE KEYS */;
$select =" INSERT INTO `tbl_serv_prov` VALUES (17,7,'2013-06-15 04:45:22','Joe\\\'s Trucking','----','N')";
/*!40000 ALTER TABLE `tbl_serv_prov` ENABLE KEYS */;
UNLOCK TABLES;......";


/* execute multi query */
if (mysqli_multi_query($mysqli_link, $query)) {
   do {
       /* store first result set */
       if ($result = mysqli_store_result($mysqli_link)) {
           //do nothing since there's nothing to handle
           mysqli_free_result($result);
       }
       /* print divider */
       if (mysqli_more_results($mysqli_link)) {
           //I just kept this since it seems useful
           //try removing and see for yourself
       }
   } while (mysqli_next_result($mysqli_link));
}
4

1 回答 1

2

让 MySQL 完成工作会更容易......

shell_exec("mysqldump -hHOST -uUSERNAME -pPASSWORD DBNAME TABLE1 TABLE2 TABLE3  > db_backup.sql");

如果要保留多个副本,可以将文件名设置为有日期。

编辑(加载文件):

shell_exec("mysql -hHOST -uUSERNAME -pPASSWORD DBNAME < filename");
于 2013-09-12T18:20:09.587 回答