[PHP-DB] Strange bahavior with mysql_fetch_array($result)
I am searching a table and have the following php code with my comments: $result = mysql_query($query); print The result == $result; // it returns The result == Resource id #2 if (!mysql_fetch_array($result)) { ... } else { while ($row = mysql_fetch_array($result)) { ... } } Neither the if nor the while get executed. Can you make any suggestions? Obviously I'm new to this. Evan -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Strange bahavior with mysql_fetch_array($result)
On Thursday 20 November 2003 08:54 pm, Evan Panagiotopoulos wrote: I am searching a table and have the following php code with my comments: $result = mysql_query($query); print The result == $result; // it returns The result == Resource id #2 if (!mysql_fetch_array($result)) { ... } else { while ($row = mysql_fetch_array($result)) { ... } } Try - $result = mysql_query($query); // Test to see if the query failed if ($result === false) { die('some meaningful message'); } // Test to see if there are results to return if (mysql_num_rows($result) 0) { // Get results while($row = mysql_fetch_array($result)) { } } else { echo 'No Results'; } -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Strange bahavior with mysql_fetch_array($result)
What of the sql statement? If your sql statement had mistakes in it (be it typo, or wrongly constructed statements), it will not even execute the if (!mysql_fetch_array($result)) { } portion of your code. You might want to construct your error checking portion differently as opposed to what you have now. Gerald Samuel's and Kirk Babb's code should fit your requirements. Gerard Samuel wrote: On Thursday 20 November 2003 08:54 pm, Evan Panagiotopoulos wrote: I am searching a table and have the following php code with my comments: $result = mysql_query($query); print The result == $result; // it returns The result == Resource id #2 if (!mysql_fetch_array($result)) { ... } else { while ($row = mysql_fetch_array($result)) { ... } } Try - $result = mysql_query($query); // Test to see if the query failed if ($result === false) { die('some meaningful message'); } // Test to see if there are results to return if (mysql_num_rows($result) 0) { // Get results while($row = mysql_fetch_array($result)) { } } else { echo 'No Results'; }