2010-10-12 157 views
0

如何在我的MySQL查詢中檢查每個數組中的每個值。我希望這是有道理的。PHP和MySQL查詢問題

SELECT articles_comments.comment_id FROM articles_comments 
WHERE articles_comments.comment_id = 

Array 
(
    [0] => 5 
    [1] => 6 
    [2] => 10 
    [3] => 11 
    [4] => 12 
    [5] => 17 
    [6] => 3 
    [7] => 4 
    [8] => 7 
    [9] => 8 
    [10] => 9 
    [11] => 16 
) 

AND articles_comments.user_id = 

Array 
(
    [2] => 4 
    [3] => 4 
    [4] => 4 
    [8] => 4 
    [9] => 4 
    [10] => 4 
) 

回答

4

就像是:

$query = "SELECT articles_comments.comment_id FROM articles_comments 
WHERE articles_comments.comment_id IN(" . implode(",", $commentArray) . ") 
    AND articles_comments.user_id IN(". implode(",", $userArray) . ")"; 

+0

什麼也沒有發生:( – HELP 2010-10-12 17:37:16

+0

希望你的執行$查詢...很高興地說,沒有任何反應,但更精確的是,給更多的代碼... – 2010-10-12 17:41:27

+0

看起來不錯,我+1 – 2010-10-12 17:45:00

0
SELECT articles_comments.comment_id 
FROM articles_comments 
WHERE articles_comments.comment_id in (1,2,3,4,5) 
AND articles_comments.user_id in (6,7,8,9) 
+0

之間做相同 – Xaqron 2010-10-12 17:37:12

+0

什麼也沒有發生:( – HELP 2010-10-12 17:38:50

+0

我得到它的工作就必須與''」。 $ array。「'''謝謝 – HELP 2010-10-12 17:56:53

0

嘗試

<?php 

$arr1 = Array(5,6,10,11,12,17,3,4,7,8,9,16); 

$arr2 = Array(4,4,4,4,4,4); 


$sql = "SELECT articles_comments.comment_id FROM articles_comments 
WHERE articles_comments.comment_id IN (" 
.implode(",",$arr1). 
") 
AND articles_comments.user_id IN (" 
.implode(",",$arr2). 
") "; 

echo $sql; 

// You still need to connect to a database, execute this query and display the results 
// This is just how you would parse the array into the query 

?> 

它給出了這樣的SQL:

SELECT articles_comments.comment_id FROM articles_comments WHERE 
articles_comments.comment_id IN (5,6,10,11,12,17,3,4,7,8,9,16) AND 
articles_comments.user_id IN (4,4,4,4,4,4) 
+0

什麼也沒有發生:( – HELP 2010-10-12 17:38:19

+1

測試,工作正常 – 2010-10-12 17:42:01

+0

複製粘貼到測試腳本,工作正常。請確保您在添加你的腳本的開始和結尾 – 2010-10-12 17:43:15