mysqli_multi_query 在多次插入时失败
您好,我遇到了以下问题:
当我尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我这样做是为了尽量减少出错的可能性
I would do this to minimize chances of error
您应该删除最后一个条目的分号。
You should remove the semicolon for the last entry.