array_intersect_key PHP array with MYSQL database
I have a simple array in PHP, say
[1, 2, 4, 5, 6, 7]
and I wish to query a MYSQL database
[1, who]
[2, where]
[3, some]
[6, there]
[9, too]
I only wish to receive the rows of intersection between the PHP array and the database's indices column. So this would result in
[1, who]
[2, w开发者_如何学Chere]
[6, there]
Any ideas? Thanks!
You want the sql in
keyword
SELECT id, title FROM tablename WHERE id IN (1, 2, 4, 5, 6, 7)
You can prepare the list of numbers using:
$nums = array(1, 2, 4, 5, 6, 7)
$sql = 'SELECT id, title FROM tablename WHERE id IN (' . implode(',', $nums) . ')';
Edit: you can make sure your input contains only numbers with array_filter
:
$nums = array_filter($nums, 'is_numeric');
精彩评论