How to use IF NULL, do this in MySQL?

Viewed 119

I'm trying to build a members-system in which you can view certain values from a database. It worked, so that was great, but I'm having a problem now because I'm using a NULL value on 'age'.

When viewing a profile page, it looks like this:

$id = $_GET["id"];

$result = mysql_query("SELECT * FROM membersfromsite WHERE `idofmember`=$_GET[id]");
$row = mysql_fetch_array($result);

echo "<b>" . $row['userusername'] . "</b>: </p>"; ?>

<?php

$id = $_GET["id"];

$result = mysql_query("SELECT * FROM membersfromsite WHERE `idofmember`=$_GET[id]");
$noage = mysql_query("SELECT ISNULL([age]) FROM membersfromsite WHERE         `idofmember`=$_GET[id]");

while ($row = mysql_fetch_array($result))
{
  echo "<p class='middle'>id-number: " . $row['idofmember'] . "<br>";
  echo "Username: " . $row['userusername'] . "<br>";
  if ($noage)
  {
    echo "Age not specified";
  }
  else
  {
    echo "Age: " .$row['age'] ;
  }
}

I have tried all kinds of other things, but the problem which I 'm having is that it either returns 'Age not specified' on every userpage or the age on every userpage, including the pages with a NULL value, which makes it look like:

Age:

The code which you can see above returns the age on every page, including the pages with an age which is set to NULL. What I don't understand is if I change the code to this:

$noage = mysql_query("SELECT * FROM membersfromsite WHERE `idofmember`=$_GET[id] AND age IS NULL");

it simply doesn't work. Since I'm using IS NULL instead of = NULL I don't really see why this shouldn't work, but I guess it has to do with the IF which is inside the 'while' thing, I don't really see in what other way I could fix this though...

I'm having an idea what the problem is, because I think that there is already a MyQS, Query done with Results and $noage is maybe ignored because of this, but I don't know how to solve this.

3 Answers
Related