What will be the result of $ when MySQL returns nothing?

This may sound ridiculously simple, but I get all sorts of errors depending on how I handle a request that returns nothing.

$query = "SELECT * FROM messages WHERE id > ".$messageId;
$result =mysql_query($query);
$time = time();
while(time()-$time<60 && $result==false)
{
    $result = mysql_query($query);
}
if(result != false)
     //Encode response
else
     //return nothing

      

How can I check if my mysql_query () was returning anything?

+2


source to share


1 answer


You can check the number of rows returned using mysql_num_rows ().

Assuming your loop has to ask for something until it gets a result, this would be

while(time()-$time<60 && $num_rows == 0)
{
    $result = mysql_query($query);
    $num_rows = mysql_num_rows($result); 

      



(not sure if what you are doing here is really a good idea, as it can put a heavy load on the database server, but that is a different problem)

mysql_query () will false

only return on "real" errors, eg. a spelling request or a lost connection.

+2


source







All Articles