<p>I have an SQL query that takes the following form:</p>

<pre><code>UPDATE foo
SET flag=true
WHERE id=?
</code></pre>

<p>I also have a PHP array which has a list of IDs. What is the best way to accomplish this other than with parsing, as follows, ...</p>

<pre><code>foreach($list as $item){  
    $querycondition = $querycondition . " OR " . $item;  
}
</code></pre>

<p>... and using the output in the <code>WHERE</code> clause?</p>

[{'answer_id': 39802, 'author': 'matt b', 'author_id': 4249, 'author_profile': 'https://Stackoverflow.com/users/4249', 'pm_score': 3, 'selected': False, 'text': '<p>You should be able to use the IN clause (assuming your database supports it):</p>\n\n<p><code>UPDATE foo\nSET flag=true\nWHERE id in (1, 2, 3, 5, 6)</code></p>\n'}, {'answer_id': 39804, 'author': 'Ryan Ahearn', 'author_id': 75, 'author_profile': 'https://Stackoverflow.com/users/75', 'pm_score': 0, 'selected': False, 'text': "<p>I haven't ever seen a way to do that other than your foreach loop.</p>\n\n<p>But, if $list is in any way gotten from the user, you should stick to using the prepared statement and just updating a row at a time (assuming someone doesn't have a way to update several rows with a prepared statement).  Otherwise, you are wide open to sql injection.</p>\n"}, {'answer_id': 39805, 'author': 'Forgotten Semicolon', 'author_id': 1960, 'author_profile': 'https://Stackoverflow.com/users/1960', 'pm_score': 1, 'selected': False, 'text': '<p>Use join/implode to make a comma-delimited list to end up with:</p>\n\n<pre><code>UPDATE foo SET flag=true WHERE id IN (1,2,3,4)\n</code></pre>\n'}, {'answer_id': 39807, 'author': 'Chris Bartow', 'author_id': 497, 'author_profile': 'https://Stackoverflow.com/users/497', 'pm_score': 4, 'selected': True, 'text': '<p>This would achieve the same thing, but probably won\'t yield much of a speed increase, but looks nicer.</p>\n\n<pre><code>mysql_query("UPDATE foo SET flag=true WHERE id IN (".implode(\', \',$list).")");\n</code></pre>\n'}, {'answer_id': 39808, 'author': 'jason saldo', 'author_id': 1293, 'author_profile': 'https://Stackoverflow.com/users/1293', 'pm_score': 0, 'selected': False, 'text': '<p>you can jam you update with case statements but you will have to build the query on your own.</p>\n\n<pre><code>UPDATE foo\nSET flag=CASE ID WHEN 5 THEN true ELSE flag END  \n    ,flag=CASE ID WHEN 6 THEN false ELSE flag END \nWHERE id in (5,6)  \n</code></pre>\n\n<p>The where can be omitted but saves you from a full table update.</p>\n'}, {'answer_id': 39809, 'author': 'csmba', 'author_id': 350, 'author_profile': 'https://Stackoverflow.com/users/350', 'pm_score': 0, 'selected': False, 'text': '<p><strong>VB.NET code:</strong>\ndim delimitedIdList as string = arrayToString(listOfIds)</p>\n\n<p>dim SQL as string = " UPDATE foo SET flag=true WHERE id in (" + delimitedIdList  + ")"</p>\n\n<p>runSql(SQL)</p>\n'}, {'answer_id': 39811, 'author': 'Michał Niedźwiedzki', 'author_id': 2169, 'author_profile': 'https://Stackoverflow.com/users/2169', 'pm_score': 3, 'selected': False, 'text': '<p>Use IN statement. Provide comma separated list of key values. You can easily do so using <code>implode</code> function.</p>\n\n<pre><code>UPDATE foo SET flag = true WHERE id IN (1, 2, 3, 4, 5, ...)\n</code></pre>\n\n<p>Alternatively you can use condition:</p>\n\n<pre><code>UPDATE foo SET flag = true WHERE flag = false\n</code></pre>\n\n<p>or subquery:</p>\n\n<pre><code>UPDATE foo SET flag = true WHERE id IN (SELECT id FROM foo WHERE .....)\n</code></pre>\n'}, {'answer_id': 39825, 'author': 'hamishmcn', 'author_id': 3590, 'author_profile': 'https://Stackoverflow.com/users/3590', 'pm_score': 0, 'selected': False, 'text': '<p>If you know a bound on the number of items then use the "IN" clause, as others have suggested:</p>\n\n<pre><code>UPDATE foo SET flag=true WHERE id in (1, 2, 3, 5, 6)\n</code></pre>\n\n<p>One warning though, is that depending on your db there may be a limit to the number of elements in the clause. Eg oracle 7 or 8 (?) used to have a limit of 256 items (this was increased significantly in later versions)<br>\nIf you do iterate over a list use a transaction so you can rollback if one of the updates fails</p>\n'}]
['https://Stackoverflow.com/questions/39792', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4224/']