开发者

array_intersect_key PHP array with MYSQL database

开发者 https://www.devze.com 2022-12-18 10:21 出处:网络
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]

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');
0

精彩评论

暂无评论...
验证码 换一张
取 消