gpt4 book ai didi

php - mysqli_multi_query 出现问题

转载 作者:行者123 更新时间:2023-11-29 19:29:34 24 4
gpt4 key购买 nike

我在使用 mysqli_multi_query 时遇到问题。我可以多次INSERT INTO,但是一旦包含DELETE,所有后续查询都将不起作用。有趣的是,当将此查询粘贴到 phpMyAdmin SQL 框中时,它可以正常工作。

SET @contact_id := 1; 
INSERT INTO `contacts_tags` (`contact_id`, `tag_id`)
SELECT * FROM (SELECT @contact_id, 1) AS tmp
WHERE NOT EXISTS (
SELECT `contact_id`, `tag_id` FROM `contacts_tags`
WHERE `contact_id` = @contact_id AND `tag_id` = 1
);
DELETE FROM `contacts_tags`
WHERE `contact_id`=@contact_id AND `tag_id` NOT IN (1);

直到 DELETE 查询的所有查询都按照我的 PHP 代码的预期执行。

添加更多查询作为日志(见下文),步骤 1、@contact_id、步骤 2a、步骤 2b 和步骤 2c 均已插入。 “最后”一个则不然。 (当然,DELETE 也没有。

INSERT INTO `tf_logs` (`query`) VALUES ('Step0');
SET @contact_id := 1;
INTO `tf_logs` (`query`) VALUES ('Step1');
INSERT INTO `contacts_tags` (`contact_id`, `tag_id`)
SELECT * FROM (SELECT @contact_id, 1) AS tmp
WHERE NOT EXISTS (
SELECT `contact_id`, `tag_id` FROM `contacts_tags`
WHERE `contact_id` = @contact_id AND `tag_id` = 1
);
INSERT INTO `tf_logs` (`query`) VALUES (@contact_id);
INSERT INTO `tf_logs` (`query`) VALUES ('Step 2a');
INSERT INTO `tf_logs` (`query`) VALUES ('Step 2b');
INSERT INTO `tf_logs` (`query`) VALUES ('Step 2c');
DELETE FROM `contacts_tags`
WHERE `contact_id`=@contact_id AND `tag_id` NOT IN (1);
INSERT INTO `tf_logs` (`query`) VALUES ('Last');

为了创建此查询,我将值增量附加到字符串中,然后使用 mysqli_multi_query,如下所示。为了准确检查变量包含的内容,我已通过电子邮件向自己发送了最终查询。将电子邮件内容复制并粘贴到 phpMyAdmin 中即可完全按照预期执行查询。

$queryString = "";
$queryString .= "INSERT INTO `tf_logs` (`query`) VALUES ('Step0'); ";
$queryString .= "
SET @contact_id := $contactId; ";
//...and so on

if(mysqli_multi_query($connection,$queryString)) {
echo "Success.";

$to = "abc@xyz.com";
$subject = "SQL query";
$headers = "From: abc@xyz.com";
mail($to,$subject,$queryString,$headers);
} else {
echo "Error.";
}

我确信我在这里遗漏了一些明显的东西,但是几天的谷歌搜索已经让我发疯了。提前致谢!

最佳答案

感谢@Chris Caviness-正如预期的那样,解决方案很简单!用户拥有 INSERT 权限,但没有 DELETE 权限。已修复并可正常工作。

关于php - mysqli_multi_query 出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41893828/

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