Best way to check if MySQL results returned in PHP?

前端 未结 11 1075
长情又很酷
长情又很酷 2020-11-28 23:21

I\'m looking for the best way to check and see if any results were returned in a query. I feel like I write this part of code a lot and sometimes I get errors, and sometimes

11条回答
  •  情话喂你
    2020-11-29 00:10

    Usually I use the === (triple equals) and __LINE__ , __CLASS__ to locate the error in my code:

    $query=mysql_query('SELECT champ FROM table')
    or die("SQL Error line  ".__LINE__ ." class ".__CLASS__." : ".mysql_error());
    
    mysql_close();
    
    if(mysql_num_rows($query)===0)
    {
        PERFORM ACTION;
    }
    else
    {
        while($r=mysql_fetch_row($query))
        {
              PERFORM ACTION;
        }
    }
    

提交回复
热议问题