Question

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.

Was it helpful?

Solution

You don't need to do a whole separate $noage query.

Just do:

if(!$row['age'])
{
echo "Age not specified";
}
else
{
echo "Age: " .$row['age'] ;
}

OTHER TIPS

Instead of if($noage) use if(!$row['age']) and skip the second query.

The other reason your code does not work is that the second query returns an array with something like array('expr1' => 0) which is not false. You only get a false result if nothing is found.

The reason why = NULL does not work? There are books written about it, it is just as it is.

Rather than relying on MySQL to tell us if no age was found or not, we can programatically determine whether the age value is Null right in PHP.

Here is an example:

$id = $_GET['id'];
$result = mysql_query("SELECT * FROM membersfromsite WHERE `idofmember` = '" . mysql_real_escape_string($id) . "'");

while($row = mysql_fetch_array($result)) {
  echo '<p class='middle'>id-number: ' . $row['idofmember'] . '<br>';
  echo 'Username: ' . $row['userusername'] . '<br>';
  if($row['age'] === Null) {
    echo "Age not specified";
  } else {
    echo "Age: " .$row['age'] ;
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top