Jeremy Ruten上面的答案很好并且执行得很快; 另一方面,它只给你行数而没有别的(如果你想要结果数据,你必须再次查询数据库).我用的是什么:
// only ask for the columns that interest you (SELECT * can slow down the query) $query = "SELECT some_column, some_other_column, yet_another_column FROM `table`"; $results = mysql_query($query, $connection); $numResults = mysql_num_rows($results); if ($numResults > 0) { // there are some results, retrieve them normally (e.g. with mysql_fetch_assoc()) } else { // no data from query, react accordingly }
您可以使用mysql_num_rows($ results)来检查是否返回了0行,或者使用这个更快的替代方法:
$query = "SELECT COUNT(*) AS total FROM table"; $results = mysql_query($query, $connection); $values = mysql_fetch_assoc($results); $num_rows = $values['total'];
或者,您只需检查mysql_fetch_assoc的结果是否为false.
$query = "SELECT * FROM `table`"; $results = mysql_query($query, $connection); $Row = mysql_fetch_assoc($results); if ($Row == false) { $Msg = 'Table is empty'; }