Why error occurs when I send multiple queries into mysqli_query?(为什么当我将多个查询发送到 mysqli_query 时会发生错误?)
问题描述
同样的请求在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 时会发生错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么当我将多个查询发送到 mysqli_query 时会发生
基础教程推荐
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何替换eregi() 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
