Checking if mysql_query returned anything or not

前端 未结 5 2113
天命终不由人
天命终不由人 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:25

    You could use mysql_num_rows($results) to check if 0 rows were returned, or use this faster alternative:

    $query = "SELECT COUNT(*) AS total FROM table";
    $results = mysql_query($query, $connection);
    $values = mysql_fetch_assoc($results);
    $num_rows = $values['total'];
    

提交回复
热议问题