简体   繁体   English

如何计算记录和显示的总数?

[英]How to Count Total Number of Records and Display?

$sql = "select count(userId) from tblUser";
$result=mysql_query($sql,$link)or die(mysql_error());
$row = mysql_fetch_array($result, MYSQL_NUM); 
echo $row[1];   

print_r($row); print_r($ row); is displaying no of records but whu echo is not working 没有显示任何记录,但是回声不起作用

You need mysql_num_rows 您需要mysql_num_rows

echo mysql_num_rows($result);

That'll give you number of rows return from query. 这将为您提供查询返回的行数。

If, however, you are using the count keyword in your query, you should modify your code like this: 但是,如果在查询中使用 count关键字,则应按以下方式修改代码:

 
 
 
  
  $sql = "select count(userId) as total from tblUser"; $result = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_assoc($result); echo $row['total'];
 
  

Update: You can also count total users like this: 更新:您也可以像这样计算总用户数:

$sql = "select userId from tblUser";
$result = mysql_query($sql) or die(mysql_error());
echo mysql_num_rows($result);

mysql_fetch_array is zero counted. mysql_fetch_array计数为零。 You will want to echo $row[0]; 您将要echo $row[0];

http://php.net/manual/en/function.mysql-fetch-array.php http://php.net/manual/zh/function.mysql-fetch-array.php

您确定数组索引吗?

echo $row[0];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM