Jump to content

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.