問題描述
同樣的請求在Adminer中沒有錯誤,但是在php中是
The same request in the Adminer has no errors, but in php is
您的 SQL 語法有錯誤;檢查手冊對應于您的 MariaDB 服務器版本以使用正確的語法靠近 'SET @lastID = last_insert_id();插入p_messages(letter_id, user_id, messa' 在第 1 行).
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 調用來運行查詢.
You are supposed to run your queries with separate API calls.
$DB->query("INSERT INTO ...");
$DB->query("SET @lastID = LAST_INSERT_ID()");
$DB->query("INSERT INTO ...");
請注意,這里實際上不需要第二個查詢,因為可以直接使用 LAST_INSERT_ID().
note that you don't actually need the second query here as LAST_INSERT_ID() can be used directly.
此外,對于任何數據庫交互,您都不應該使用名為HTML 特殊字符"的函數.您必須改用準備好的語句.
Besides, you should never use a function named "HTML speacial chars" for any database interaction. You have to use prepared statements instead.
請注意,使用 multi_query 的建議是不合理且具有誤導性的,會導致很多問題.
Note that a suggestion to use multi_query is unjustified and misleading, causing a lot of problems.
這篇關于為什么當我將多個查詢發送到 mysqli_query 時會發生錯誤?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!