mysqli_multi_query 在多次插入时失败

发布于 2024-10-07 10:14:20 字数 1427 浏览 1 评论 0原文

您好,我遇到了以下问题:

当我尝试使用 mysqli_multi_query 函数执行多个 INSERT INTO 查询时,没有一个被执行,并且我收到一个标准的“您的 SQL 语法中有错误”错误,但是当我采取我传递给函数并粘贴到 PHPMyAdmin 并执行它的完全相同的字符串,它工作完美!

以下是我尝试执行的查询:

INSERT INTO production VALUES( 120, 103, 10, 0, 0 ); 
INSERT INTO production VALUES( 120, 107, 5, 1, 0 ); 
INSERT INTO production VALUES( 120, 106, 7, 2, 0 ); 
INSERT INTO production VALUES( 120, 103, 20, 0, 1 );

这些查询位于单个字符串中,在分号后用空格分隔。

这是我在代码中所做的:

$querytext = $queries // Get all the queries
$query_result = mysqli_multi_query( $this->_connection, $querytext );

if( mysqli_errno($this->_connection) > 0)
    echo mysqli_error($this->_connection);

var_dump( $querytext );
var_dump( $query_result );

执行此代码会产生:

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在 'INSERT INTO production VALUES( 120, 107, 5, 1, 0 ); 附近使用的正确语法。在第 1 行插入生产值'

string(210) "插入生产值( 120, 103, 10, 0, 0 ); 插入生产值( 120, 107, 5, 1, 0 ); 插入生产值( 120, 106, 7 , 2, 0 ); 插入生产值( 120, 103, 20, 0, 1 );

布尔值(假)

如果您想自己测试此行为,这里是 product table:

CREATE TABLE `production` (
`colonyID` INT NOT NULL ,
`resource_type_being_built` INT NOT NULL ,
`amount_requested` INT NOT NULL ,
`build_list_position` INT NOT NULL ,
`production_number` INT NOT NULL ,
INDEX (  `colonyID` )
) ENGINE = MYISAM ;

我是否忽略了某些事情,或者这只是奇怪的行为?

Greetings, I'm having the following issue:

When I attempt to execute several INSERT INTO queries using the mysqli_multi_query function, none of them are executed and I receive a standard "You have an error in your SQL syntax" error, but when I take the exact same string I passed to the function and paste into PHPMyAdmin and execute it, it works flawlessly!

Here are the queries I am attempting to execute:

INSERT INTO production VALUES( 120, 103, 10, 0, 0 ); 
INSERT INTO production VALUES( 120, 107, 5, 1, 0 ); 
INSERT INTO production VALUES( 120, 106, 7, 2, 0 ); 
INSERT INTO production VALUES( 120, 103, 20, 0, 1 );

These are in a single string, separated by a space after the semicolon.

Here's what I do in the code:

$querytext = $queries // Get all the queries
$query_result = mysqli_multi_query( $this->_connection, $querytext );

if( mysqli_errno($this->_connection) > 0)
    echo mysqli_error($this->_connection);

var_dump( $querytext );
var_dump( $query_result );

Executing this code results in:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO production VALUES( 120, 107, 5, 1, 0 ); INSERT INTO production VALUE' at line 1

string(210) "INSERT INTO production VALUES( 120, 103, 10, 0, 0 ); INSERT INTO production VALUES( 120, 107, 5, 1, 0 ); INSERT INTO production VALUES( 120, 106, 7, 2, 0 ); INSERT INTO production VALUES( 120, 103, 20, 0, 1 ); "

bool(false)

If you would like to test this behaviour out yourself, here is the production table:

CREATE TABLE `production` (
`colonyID` INT NOT NULL ,
`resource_type_being_built` INT NOT NULL ,
`amount_requested` INT NOT NULL ,
`build_list_position` INT NOT NULL ,
`production_number` INT NOT NULL ,
INDEX (  `colonyID` )
) ENGINE = MYISAM ;

Am I overlooking something or is this simply odd behaviour?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木槿暧夏七纪年 2024-10-14 10:14:20

我这样做是为了尽量减少出错的可能性

INSERT INTO production VALUES (120,103,10,0,0), 
(120,107,5,1,0), (120,106,7,2,0), (120, 103,20,0,1);

I would do this to minimize chances of error

INSERT INTO production VALUES (120,103,10,0,0), 
(120,107,5,1,0), (120,106,7,2,0), (120, 103,20,0,1);
嗼ふ静 2024-10-14 10:14:20

您应该删除最后一个条目的分号。

INSERT INTO production VALUES( 120, 103, 10, 0, 0 ); 
INSERT INTO production VALUES( 120, 107, 5, 1, 0 ); 
INSERT INTO production VALUES( 120, 106, 7, 2, 0 ); 
INSERT INTO production VALUES( 120, 103, 20, 0, 1 )

You should remove the semicolon for the last entry.

INSERT INTO production VALUES( 120, 103, 10, 0, 0 ); 
INSERT INTO production VALUES( 120, 107, 5, 1, 0 ); 
INSERT INTO production VALUES( 120, 106, 7, 2, 0 ); 
INSERT INTO production VALUES( 120, 103, 20, 0, 1 )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文