Tuesday, March 19, 2013

check if mysql query returns empty in PHP



We can use PHP  function mysql_num_rows to check if mysql query returns empty.
Example code:
$sql    = "SELECT * FROM cesei_member_extra WHERE member_id = $id";
  $result = mysql_query($sql, $db);

if ($result) {
     if (mysql_num_rows($result) == 0) {
        echo "No result returns";     }
     else {
        echo  mysql_num_rows($result).'  rows returned';
     }
}

else{
      echo "Query error";
}
If the query is not empty, we can use mysql_fetch_assoc to get the result
    $enrollment_list         = Array();
    while($row = mysql_fetch_assoc($result)){ 
                    $enrollment_list[] = $row['member_id'];
           
             }

No comments:

Post a Comment