1

我在 PHP 中有一个简单的数组,比如说

[1, 2, 4, 5, 6, 7]

我想查询一个MYSQL数据库

[1, who]
[2, where]
[3, some]
[6, there]
[9, too]

我只希望接收 PHP 数组和数据库索引列之间的交叉行。所以这会导致

[1, who]
[2, where]
[6, there]

有任何想法吗?谢谢!

4

1 回答 1

4

你想要 sqlin关键字

SELECT id, title FROM tablename WHERE id IN (1, 2, 4, 5, 6, 7)

您可以使用以下方法准备数字列表:

$nums = array(1, 2, 4, 5, 6, 7)
$sql = 'SELECT id, title FROM tablename WHERE id IN (' . implode(',', $nums) . ')';

编辑:您可以确保您的输入仅包含以下数字array_filter

$nums = array_filter($nums, 'is_numeric');
于 2010-01-27T15:57:00.947 回答