2010-01-27 21 views
1

我在PHP簡單數組,說array_intersect_key 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

你想要的SQL in關鍵字

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

可能要添加一個關於SQL注入的危險的巨大聲明e ... – 2010-01-27 16:01:26

+0

非常感謝,你們倆。我知道IN運營商,但只要你發佈,我就知道該怎麼做。我也一定會檢查注入漏洞。 – Booker 2010-01-27 16:10:28

+0

謝謝,添加array_filter以確保$ nums只包含數字 – 2010-01-27 16:38:25