RE: If select query doesn't net any results

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> ...
> if ($result) {
> 	while ($row...
> 	...
> }
> 
> $result will be 0 (false) if nothing satisfies the query.  Any other
> comparison is just extra typing with no improvement in logic.

No, it only returns false/0 if the query fails, meaning there was an
error and the query couldn't be executed. The query can execute just
fine, return no rows, and so the result would be true. 

What you have is extra typing with no improvement in logic. ;)

If you need to know the number of rows returned, then use the
if(mysql_num_rows() method. If you don't, then you can do this:

if($row = mysql_fetch_row($result))
{
	do{
		//do whatever with $row data
	}while($row = mysql_fetch_row($result));
}
else
{ echo "no rows returned from query"; }

If you simply do not want something done if no rows are returned, then
the simple while($row = mysql_fetch_row($result)) method works fine, as
the while() will only be true if rows were returned and skipped if no
rows are returned.

---John W. Holmes...

PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/



-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux