1

So i have a insert query with multiple rows (1 000 row per query) and it work fine, so tried to make the same for update query but i didn't find any solution,

this is my code :

$sth = $db->prepare("UPDATE table SET salutation = ? , fnamn = ? , enamn = ? , mob = ? WHERE id = ?"); for ($i=0; $i < count($recipents); $i++) { if(isset($recipents[$i]['id'])) $sth->execute(array($recipents[$i]['salutation'], $recipents[$i]['fnamn'], $recipents[$i]['enamn'], $recipents[$i]['mob'],$recipents[$i]['id'])); } 

so please if someone has any idea how to update multiple rows in one query i will be very appreciative.

8
  • In this exemple they use ON DUPLICATE KEY or i do not update with the key, i can't do that Commented Mar 25, 2016 at 11:01
  • You have to use prepared statements for this. To wrap all the updates in a transaction also would be a good idea. Commented Mar 25, 2016 at 11:02
  • "or i do not update with the key" - id is not a unique key? Strange.... Commented Mar 25, 2016 at 11:03
  • The code you have shoud work. If you wrap it in a transaction and commit every 1000 rows it will probably be quick enough. Commented Mar 25, 2016 at 11:10
  • 1
    Sample, untested code here: pastebin.com/4E09U7cZ. IF you benchmark it you will see it is quite quick compared with using autocommit. Commented Mar 25, 2016 at 11:31

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.