I am running a script in PHP that uisng a loop creates a string query for MySQL.
After executing the script I get the following error:
"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 'UPDATE BANNERS SET pos=1 WHERE BID=5; UPDATE BANNERS SET pos=2 WHERE BID=1' at line 2"
right after the error I echo the query and it looks like this:
UPDATE BANNERS SET pos=0 WHERE BID=6;
UPDATE BANNERS SET pos=1 WHERE BID=5;
UPDATE BANNERS SET pos=2 WHERE BID=1;
When I copy and paste it into phpmyadmin, it obviously gets executed without any problem.
Any Ideas?
Here is the PHP code:
There is an array that looks like this:
$order[0] = 'tr_6';
$order[1] = 'tr_5';
$order[2] = 'tr_1';
$query = "";
foreach($order as $pos => $value){
$idvalue = str_replace('tr_','',$value);
$query .= "UPDATE BANNERS SET pos=$pos WHERE BID=$idvalue;\n";
}
mysqli_query($connection,$query) or die(mysqli_error($connection)."<br/>$query");
Thanks!
mysqli allow multiple queries with mysqli_multiple_query function like this:
note that you need to use semicolon after each query.