Jump to content

[SOLVED] Problem with If Else from null queries


jedispyder

Recommended Posts

I have a message I'm trying to return when I get a null result after doing a query, but can't seem to get it to work. I think the problem is finding out which variable to use to test when something is null or if I'm just testing it wrong.

 

My code:

//$result is the result of the query
if ($result != "") {
      while($row = mysql_fetch_array($result))
      {
            echo "Results returned.";
      }
} else {
   echo "Sorry, but no results were returned.";
}

 

It properly returns results when something is returned, but if nothing is returned then it doesn't return the "Sorry, but not results were returned."

 

Another code I tried that still didn't work was:

if (!$result) {
   echo "Sorry, but no results were returned.";
}

 

I've tried including it within the While and using $row but still haven't had any luck. Any help?

Ken2k7 > Sorry for putting it in the wrong forum, I saw other posts on problems with "If" here and thought this would be the right place. Sorry!

 

Maq > I tried using that and it didn't work =o/

 

And I did look up mysql_query and tried several of those combos but none worked.

Dont use "if ($result != "")", that checks its mysql recordset, not the actual data you want.  So even if there are no results, you'll still get a recordset back.

 

instead after you do your query, try this:

 

if (mysql_num_rows($sql) > 0)  {

  //code...

  }

Dont use "if ($result != "")", that checks its mysql recordset, not the actual data you want.  So even if there are no results, you'll still get a recordset back.

 

instead after you do your query, try this:

 

if (mysql_num_rows($sql) > 0)  {

  //code...

  }

Thanks, it works perfectly now!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.