mysqli_multi_query在多次插入时失败 [英] mysqli_multi_query fails on multiple inserts

查看:106
本文介绍了mysqli_multi_query在多次插入时失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,我遇到以下问题:

Greetings, I'm having the following issue:

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

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 );

执行此代码将导致:

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

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)将插入值插入生产值(120,103,10,0,0);将插入值插入生产值(120,107,5,1,0);将插入值插入生产值(120,106,7 ,2,0);插入生产值(120,103,20,0,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 ); "

布尔(假)

如果您想亲自测试一下此行为,请参见production表:

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?

推荐答案

我这样做是为了最大程度地减少出错的机会

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);

这篇关于mysqli_multi_query在多次插入时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆