PHP - mysql_num_rows()期望参数1是资源

时间:2012-10-27 01:50:47

标签: php mysql

$query = "SELECT * FROM XXXX";
$result = mysql_query($query);
$noOfRows = mysql_num_rows($result);

如何在获得$result的结果之前检查mysql_num_rows是否为空?

1 个答案:

答案 0 :(得分:1)

您可以使用三元运算符检查$result并将$noOfRows设置为0,如果它是false

$query = "SELECT * FROM XXXX";
$result = mysql_query($query);
$noOfRows = (false===$result)? 0 : mysql_num_rows($result);