tags:

views:

61

answers:

3

How can I check each value in 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
)
A: 
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)
Tim
BETWEEN do the same as IN
Xaqron
Nothing happens:(
blah
I got it to work it had to with `'" . $array . "'` thanks
blah
+4  A: 

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

?

Arnaud F.
Nothing happens:(
blah
Hope your executing $query... It's good to say that nothing happens but be more precise, give more code...
Arnaud F.
Looks good to me +1
Phill Pafford
query comes out empty when there should be some values
blah
When you paste the content of $query in PHPMyAdmin (or something else), you retrieve the result? Add the end delimiter ";" in $query, maybe it'll help.
Arnaud F.
I got it to work it had to with `'" . $array . "'` thanks
blah
A: 

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) 
Phill Pafford
Nothing happens:(
blah
tested and works fine
Phill Pafford
copy and paste into test script, works fine. make sure you add the <?php and ?> at the start and end of your scripts
Phill Pafford
query comes out empty when there should be some values
blah
That's not your question, if you need to debug your query results then you should post the table structure as well as the expected results and the query you're executing
Phill Pafford
@Phill Pafford I know thats not the original question but when I use your method thats what happens
blah
I would suggest using a MySQL GUI Tool to connect to your database and execute the query manually. Tweak it till it gives you the results and then create the code to build the query
Phill Pafford
I got it to work it had to with `'" . $array . "'` thanks
blah