I need to do batch MYSQL insert/updates. I got batch insert statement to work, but when the insert comes as multiple one liners it does not.. Similarly I have not been able to generate a batch update. Please see examples below.
Batch insert statement works
$sql = "INSERT INTO `test` (`somefield`) VALUES ('test', 'test');";
db::statement($sql);
Multiple separate insert statements NOT working
$sql = "INSERT INTO `test` (`somefield`) VALUES ('test'); INSERT INTO `test` (`somefield`) VALUES ('test');";
db::statement($sql);
SQLSTATE[42000]: Syntax error or access violation: 1064 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
test
(somefield
) VALUES ('test')' at line 1 (SQL: INSERT INTOtest
(somefield
) VALUES ('test'); INSERT INTOtest
(somefield
) VALUES ('test');)
Batch update statement not working
$sql = "INSERT INTO 'flights' (`id`, `airline`) VALUES ('142832', 'BA') ON DUPLICATE KEY UPDATE `airline`=VALUES(`airline`);"
db::statement($sql);
1064 - 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 ''flights' (
id
,airline
) VALUES ('142832', 'BA') ON DUPLICATE KEY UP' at line 1
Reviewed multiple Stackoverflow posts - but I am getting something wrong
Multiple insert statements - Multiple SQL Update Statements in single query
Batch update statement - Multiple Updates in MySQL
Would appreciate help on this - thanks!