如何在while循环PHP中只显示最终结果?

时间:2014-09-21 11:41:08

标签: php mysql

如果我在数据库中创建一个循环遍历一定数量的id,我怎么能只回显一次而不是每次循环通过id?我的代码显示在:

$query = "SELECT userid FROM users ";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result))
{
echo "There are currently <b> ". $row['userid'] ."</b> users registered in our server database!";
echo "<br />";  
}

3 个答案:

答案 0 :(得分:0)

您应该SELECT COUNT()来获取用户数。

答案 1 :(得分:0)

我意识到你只想要用户数

你应该使用

$query = "SELECT count(*) as count FROM users ";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result))
{
echo "There are currently <b> ". $row['count'] ."</b> users registered in our server database!";
echo "<br />";  
}

答案 2 :(得分:0)

您可以使用COUNT(userid)代替mysql_fetch_array()

$query = "SELECT COUNT(userid) FROM users";
$result = mysql_query($query) or die(mysql_error());

如果是MAX(userid)类型

,也可以使用int
$query = "SELECT MAX(userid) as total_users FROM users";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$total_user = $row['total_users'];