开发者

PHP & MySQL query question

How can I check each value i开发者_开发百科n each array in my MySQL query. I hope this makes sense.

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
)


Like that :

$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) . ")";

?


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)


Try

<?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

?>

It gives this for the 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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜