Re: determining number of rows in a mysql table

From: Date: Fri, 02 Apr 2004 17:28:54 +0000
Subject: Re: determining number of rows in a mysql table
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-182151@lists.php.net to get a copy of this message
-{ Rene Brehmer }- wrote:
Thought it looked fishy .... hehe ... My only problem is: Sometimes you actually need the data from that table later in the same script, so instead of doing another data pull, I have alot of cases where this is alot more useful (not actual code from any of my work, but I've used this structure in the past): $query = mysql_query("SELECT $fields FROM $table WHERE $crit") or die('fetch error<br>'.mysql_error()); if (mysql_num_rows($query) > 0) { $result = mysql_fecth_array($query); // and so on ... }
Use this code instead: if ($result = mysql_fetch_array($query)) { // do something } BTW, pls remember that an array of nothing (array()) is FALSE when it is casted into boolean. The comparison '!== FALSE' is not necessary just because mysql_fetch_array does not return empty arrays.

« previous php.general (#182151) next »