I noticed that a lot of tutorial instructions often have this code:
$sql="SELECT * from table";
$num=mysql_num_rows();
if ($num > 0)
{
do something
}
Why do they have to use that condition “if ($num > 0)” when I write this code that I think is compact and readable:
$sql="SELECT * from table";
$itExists=mysql_num_rows();
if ($itExists)
{
do something
}
does mysql_num_rows ever return a negative integer? Doesn’t PHP always equate 0 to false? I’ve used this approach and noticed nothing different or unusual.
Why would you assign the number of rows returned to a variable with a
boolname likeitExists? By doing this you are making information that could be used later less useful. It’s better to assign the number of rows (mysql_num_rows()) to a variable that says it holds a number of rows (numRowsor in this casenum).Having something like
itExistsassigned to 4, 5 or 6 isn’t good practice. It’s in this way that the first method is better.