当数据输入到我的网络表单中时,我使用mysql_real_escape_string()来逃避坏东西,并使用PHP将其存储在MySQL中。 我现在设置了一个脚本来创建备份SQL文件,以防我必须重新加载数据。
问题:诸如"...乔的卡车..."在插入语句中导致我下面的代码失败。 有没有更好的方法来自动备份和还原mysql? 或者,也许我该如何修复导致还原在此时失败的数据?
$mysqli_link = mysqli_connect("localhost","xxxx","xxxxxx","xxxxxxx");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %sn", 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));
}
让MySQL完成工作会更容易...
shell_exec("mysqldump -hHOST -uUSERNAME -pPASSWORD DBNAME TABLE1 TABLE2 TABLE3 > db_backup.sql");
如果要保留多个副本,可以将文件名设置为具有日期。
编辑(加载文件):
shell_exec("mysql -hHOST -uUSERNAME -pPASSWORD DBNAME < filename");