Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to test for zero 1

Status
Not open for further replies.

leegold2

Technical User
Oct 10, 2004
116
$num_rows = 0;
If ($num_rows = 0) {....}

This does not work, but if I set it to 1 (one) it works. How do I test for zero?

Thanks
 
You should be writing:
Code:
if ($numrows == 0) { ... }

Notice the two equal signs.

Ken
 
Just to expand a little on kenrbnsn's answer:

Think of these operators as:

A single equal sign (=) means "becomes".
A double equal sign (==) means "is equivalent to".

So,
Code:
if($num_rows = 0)
sets the value of $num_rows to 0. The if statement evaluates 0 as "false". Any nonzero value evaluates to "true" -- that's why the value 1 works but 0 doesn't.
 
Indeed, thanks for the help...should of known...
 
I would however use:

Code:
if (mysql_num_rows($result) > 0)
  {
    // do something
  }
else
  {
    echo "Sorry, no data found!";
  }

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top