0

I have a query that collects multple ID numbers and stores them into a php Array. I then want to make a mysql query for every result in the php Array. I believe I am on the right track but am getting an error saying the join() method has invalid arguments. $IdArray is a valid array and does have Ids stored in it, I am 100% sure. Here is my code,

    $secondArray = array();
    $ids = join(',', $IdArray);  
    $query = "SELECT * FROM test WHERE user = '$user' AND id IN ($ids)";
    $results = mysql_query($query);
    while($row = mysql_fetch_array($results)){
        $secondArray[] = "YES";
    }

another problem is I want a piece of information to be stored into $secondArray even if a result hasnt been found. To make it clear lets say the $ids are like this ['0','1','2','3'] and the table test only has the id 2 stored in it. But I want the $secondArray to look like this ['NO','NO','YES','NO']

5
Contributors
4
Replies
23
Views
3 Years
Discussion Span
Last Post by broj1
0

I would re-structure to just query on the user and then compare the returned ids to your array of ids

$secondArray = array();
//$ids = join(',', $IdArray);  
$query = "SELECT * FROM test WHERE user = '$user'";
$results = mysql_query($query);
while($row = mysql_fetch_array($results)){
    if (in_array($row['id'], $IdArray)) {
        $secondArray[] = "YES";
    } else {
        $secondArray[] = "No";
    }
}
0

Instead of join you may try implode, see how it works

Thought they were the same. ANyway couldn't check Google says php.net infected with malware 8|

0

I would check whether the $IdArray exists and is not empty:

$query = "SELECT * FROM test WHERE user = '$user'";
if(isset($IdArray) && !empty($IdArray)) {
    $ids = implode(',', $IdArray);
    $query .= " AND id IN ($ids)";
}
...

And, maybe you should post the $IdArray here (using print_r($IdArray)). Just to check the structure.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.