简体   繁体   中英

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); is displaying no of records but whu echo is not working

You need 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:

 
 
 
  
  $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. You will want to echo $row[0];

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

您确定数组索引吗?

echo $row[0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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