0

How do I make it pick all results that are not equal to the $var , here's my code.

$opti=mysql_query("SELECT * FROM table1 WHERE imageid=$image_id");
    while ($vari = mysql_fetch_array($opti)) {
        $var = $vari['tagid'];
    $options=mysql_query("SELECT * FROM table WHERE id!=$var");
    while ($taghe1 = mysql_fetch_array($options)) {
     $tagname = $taghe1['name'];
     echo "".$tagname.", ";
    } }    
PHP Nexter
  • 39
  • 6

3 Answers3

1

Try:

$options=mysql_query("SELECT * FROM table WHERE id<>{$var}");
Michal Przybylowicz
  • 1,558
  • 3
  • 16
  • 22
0

You could use this:

$options=mysql_query("SELECT * FROM table WHERE id not in ('$var')");

You could have multiple values here, e.g.

$options=mysql_query("SELECT * FROM table WHERE id not in ('$var1', '$var2', '$var3')");
0

You can probably see from the answer you accepted that adding the quotes solved your problem. Another way to do this is to just use one query. I will show an example using mysqli instead of the deprecated mysql, but the same query should work in mysql if you must use it. I added a couple of other suggestions that aren't really addressing your question, but make me feel better about my answer.

// Please be sure to escape $image_id before using it like this
$unused_tags = mysqli_query($db, "SELECT `name` FROM `table` AS t 
    LEFT JOIN (SELECT tagid FROM table1 WHERE imageid=$image_id) AS t1 
    ON t.id = t1.tagid WHERE t1.tagid IS NULL;");

while ($tag = mysqli_fetch_array($unused_tags)) {
    $tags[] = htmlspecialchars($tag['name']); // escape your output
}
echo implode(", ", $tags); // doing it this way eliminates the trailing comma
Don't Panic
  • 41,125
  • 10
  • 61
  • 80