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']

Recommended Answers

All 4 Replies

Can you post your table structure.

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

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";
    }
}
Member Avatar for diafol

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|

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.