How to use an array directly in sql query to update multiple rows

I know that you can update multiple rows using the following query

UPDATE MyProject\Model\User u SET u.password = 'new' WHERE u.id IN (1, 2, 3)

I doubt that if I have an array $ array = [1,2,3], I can use this array directly in the request without repeating, is this possible?

+4
source share
1 answer

try it

$sql = "UPDATE `MyProject` INNER JOIN `Model` INNER JOIN `Model` SET password='new' WHERE id IN(1,2,3)";

If you have an array ids, you can use it inIN

$array_of_id = array(1,3,5,8);
$in_text = implode(",", $array_of_id);
$sql = "UPDATE `MyProject` INNER JOIN `Model` INNER JOIN `Model` SET password='new' WHERE id IN($in_text)";
+1
source

Source: https://habr.com/ru/post/1528239/


All Articles