0

I need to update multiple rows (could be hundreds) Can I combine this into single query somehow? (And of course should I?)

$arr = array(11,32,113,4,15);//dynamic size
$playlist_id = 2;

$stmt = $db->prepare("UPDATE media SET order_id=? WHERE playlist_id=? AND id=?");

for($i=0;$i<count($arr);$i++) {

    $stmt->bind_param('iii', $i, $playlist_id, $arr[$i]);
    $stmt->execute();

}
Toniq
  • 4,492
  • 12
  • 50
  • 109
  • 2
    Possible duplicate of [Multiple Updates in MySQL](http://stackoverflow.com/questions/3432/multiple-updates-in-mysql) – Rémi Bonnet Jan 29 '17 at 22:33
  • I was looking at this http://stackoverflow.com/questions/3432/multiple-updates-in-mysql but it doesnt explain how to use it with array values. I also found this http://stackoverflow.com/questions/15149157/best-way-to-insert-many-values-in-mysqli and this http://stackoverflow.com/questions/907806/php-mysql-using-an-array-in-where-clause – Toniq Jan 29 '17 at 23:34
  • I still dont know how would write it, this requires placeholders for both order_id and id, whose values come from arrays. – Toniq Jan 30 '17 at 13:36

0 Answers0