Monday, July 9, 2018

php mysqli - burying transactions in a single query



MySQli keeps stating that there is a syntax error in the first select part of my 'combined' query as shown below:




$my_massive_query =
'START TRANSACTION WITH CONSISTENT SNAPSHOT;
SELECT d.id AS `Location ID`, ...
SELECT d.id AS `Location ID`, ...
SELECT d.id AS `Location ID`, ...
SELECT d.id AS `Location ID`, ...
SELECT d.id AS `Location ID`, ...
COMMIT';
$mysqli->query($my_massive_query);



Is this 'not allowed' in php? Does one have to use the mysqli functions like follows:



$mysqli->autocommit(FALSE);
$mysqli->query("SELECT ...");
$mysqli->query("SELECT ...");
$mysqli->query("SELECT ...");
$mysqli->commit();



I have executed the query in mysql (after using var_dump to fetch it as-is), and it is executing perfectly so I don't think there really is a syntax issue and that maybe one is not allowed to 'combine' queries in a single string?



I am worried that START TRANSACTION WITH CONSISTENT SNAPSHOT; has some extra conditions that will not be met by just setting autocommit to false.


Answer



mysqli::query can only deal with 1 query at a time. To do multiple queries in one string, use mysqli::multi_query.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...