gpt4 book ai didi

php - 为什么当我向 mysqli_query 发送多个查询时会出现错误?

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

同样的请求在Adminer中没有错误,但在php中却是

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SET @lastID = last_insert_id(); INSERT INTO p_messages(letter_id, user_id, messa' at line 1).

PHP:

$DB->query("INSERT INTO p_letters(user_1_id, user_1_name, create_date) VALUES ('".htmlspecialchars($accountId)."', '".htmlspecialchars($username)."', now()); SET @lastID = LAST_INSERT_ID(); INSERT INTO p_messages(letter_id, user_id, message) VALUES (@lastID, '".htmlspecialchars($accountId)."', '".htmlspecialchars($text)."');");

SQL:

INSERT INTO p_letters(user_1_id, user_1_name, create_date) VALUES ('acc583bfa62de6f66.05116379', '212312313', now()); SET @lastID = LAST_INSERT_ID(); INSERT INTO p_messages(letter_id, user_id, message) VALUES (@lastID, 'acc583bfa62de6f66.05116379', 'Проверка');

最佳答案

您应该使用单独的 API 调用来运行查询。

$DB->query("INSERT INTO ...");
$DB->query("SET @lastID = LAST_INSERT_ID()");
$DB->query("INSERT INTO ...");

请注意,您实际上不需要此处的第二个查询,因为可以直接使用 LAST_INSERT_ID()。

此外,您永远不应该使用名为“HTML特殊字符”的函数进行任何数据库交互。您必须改用准备好的语句。

请注意,使用 multi_query 的建议是不合理且具有误导性的,会导致很多问题。

关于php - 为什么当我向 mysqli_query 发送多个查询时会出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40844497/

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