Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have code like this:

$p = $db->query("SELECT ads_id FROM ads_submissions WHERE status = '0'");
$c_ads = array();
while($row = $db->fetchAll($p))
{
    $c_ads[] = $row;
}

Output

4 6 9

I want to add this line to $new_ads = $db-> after WHERE

AND ads_id = !in_array('$output', $c_ads)

To select all ads_id except id 4 and 6 and 9

$new_ads = $db->fetchOne("SELECT ads_id FROM ads_pack WHERE allowed_countries LIKE '%".$country_tr."%'");

But this never work correctly.

Any help please ?

Thank you.

share|improve this question
what is your goal? why select all ads with status '0' and afterwards select all not in this result? Wouldn't it be easier to change the where clause?? – sailingthoms Apr 11 at 19:35
@sailingthoms, the OP is referencing different tables in the two queries. Although a JOIN might work here. – Phill Sparks Apr 11 at 19:45
using a join would definitely a better way and much more performant – sailingthoms Apr 11 at 19:50

3 Answers

up vote 1 down vote accepted

you should do it like this

AND ads_id NOT IN ( implode(',', $c_ads) )
share|improve this answer

There's no such thing as in array in sql. There's simply result sets. you want

AND ads_id NOT IN (4,6,9)
share|improve this answer
So i just need to use AND ads_id NOT IN ($c_ads) without any changes ? – user2203703 Apr 11 at 19:31
well, you'd still have to implode the array. you can't embed a PHP array in sql directly. e.g. $sql = "... WHERE field NOT IN (" . implode(',', $array) . ")". – Marc B Apr 11 at 19:33

Could you rewrite this using a subquery, instead of running 2 queries?

"SELECT ads_id FROM ads_pack WHERE allowed_countries LIKE '%".$country_tr."%' AND ads_id NOT IN (SELECT ads_id FROM ads_submissions WHERE status = '0')"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.