gpt4 book ai didi

php mysql 在 INSERT 语句中转义字符

转载 作者:行者123 更新时间:2023-11-29 08:23:05 25 4
gpt4 key购买 nike

当数据输入到我的 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));
}

最佳答案

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

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

如果您想保留多个副本,可以将文件名设置为包含日期。

编辑(加载文件):

shell_exec("mysql -hHOST -uUSERNAME -pPASSWORD DBNAME < filename");

关于php mysql 在 INSERT 语句中转义字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18771429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com