问题
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?
回答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);
回答2:
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 )
来源:https://stackoverflow.com/questions/4451335/mysqli-multi-query-fails-on-multiple-inserts