Thursday, May 5, 2011

Unknown problem/question with: if($numrows==0)

The following code continues to be displayed even if there are entries in my database and I don't understand why. Am I missing something? I'm not sure if this makes sense, but help would be great. :)

if($numrows==0)
{
echo"<h3>Results</h3>";
echo"<p>Sorry, your search: &quot;".$escaped."&quot; returned zero results</p>";
}
From stackoverflow
  • how do you get $numrows? It can be the mistake.

    What I usually do is:

    if($numrows > 0 ){
    
      // Code
    
    }else{
      echo"<h3>Results</h3>";
      echo"<p>Sorry, your search: "".$escaped."" returned zero results</p>";
    }
    
    Holly : Oh. Thank you. I would just put my query where you have //Code right? Because when I do that it just displays the else statement. :/
    fesja : can you paste your query and how you get the $numrows value?
    barfoon : No //code is what you want to do when you have some rows. You have to query the DB first, in order to get numrows.
  • Try:

    echo "'$numrows'";
    

    Directly above the if statement. If output is not '0', then the problem is how you're assigning 0 to $numrows.

    John Rasch : The if block actually would currently get executed if $numrows were null (because the == operator doesn't compare types), but it would not if she changed it to if($numrows === 0)
    Babiker : Thaks John. You're right.
  • If the code you're having a problem with is the same as quoted in your previous question... then the problem is here:

    $numresults=mysql_query($query);
    $numrows=mysql_num_rows(numresults);
    

    You're missing a $ before numresults on the second line.

    John Rasch : +1 - nice find!

0 comments:

Post a Comment