PHP获取行数

时间:2014-01-31 13:17:48

标签: php sql database pdo rowcount

$count = mysql_query("SELECT COUNT(id) FROM users");

echo $count;

这不起作用。错误消息资源ID未知#21 如何解决这个问题?

2 个答案:

答案 0 :(得分:4)

您应该使用fetchColumn()

$count = $db->query("SELECT COUNT(id) FROM users");

应该是

$count = $db->query("SELECT COUNT(id) FROM users")->fetchColumn();

echo $count; //Returns number of rows

答案 1 :(得分:0)

使用此mysql_num_rows

$sql=mysql_query("SELECT COUNT(id) FROM users");
$count= mysql_num_rows($sql);
echo $count ;