Checking if mysql_query returned anything or not

前端 未结 5 2128
天命终不由人
天命终不由人 2021-02-18 16:56
$query = \"SELECT * FROM `table`\";
$results = mysql_query($query, $connection);

If \'table\' has no rows. whats the easiest way to check for this.?

5条回答
  •  感情败类
    2021-02-18 17:28

    One thing i noticed that was missed was the fact that the query might not succeed, so you do need to check if the $results variable is set. I'll use the answer given by yjerem as an example.

    $query = "SELECT COUNT(*) AS total FROM table";
    $results = mysql_query($query, $connection);
    if ($results) { // or use isset($results)
    $values = mysql_fetch_assoc($results);
    $num_rows = $values['total'];
    }
    

提交回复
热议问题