简体   繁体   中英

how to print mysql_num_rows?

i have a database structure like this 在此处输入图片说明

and am trying to print the rows count but am getting 1 only

the code am using is

$sql="select Count(*) from uniqueviews where hour=13"; $result=mysql_query($sql); $ml=mysql_num_rows($result); echo $ml;

according to the query it should print 6 but its printing 1

where am doing wrong ?

i think its counting the rows ?

how can i print the result after rows count ?

Count(*) only returns one row, containing the number of rows. That's why you get only 1 as return value. If you want the actual value, you can either do this:

$sql="select * from uniqueviews where hour=13"; 
$result=mysql_query($sql); 
$ml=mysql_num_rows($result); 
echo $ml;

or this:

$sql="select COUNT(*) from uniqueviews where hour=13"; 
$result=mysql_query($sql);  
$row = mysql_fetch_array($result, MYSQL_NUM);
$ml=$row[0];
echo $ml;

实际上,您正在打印查询返回的行数。由于要计算结果,因此应使用mysql_fetch_array来获取查询结果。

 $result = select Count(*) from uniqueviews where hour=13 // $result is 6 here

 and when you use mysql_count_rows($result) it returns 1 which is correct

 try printing $result which should be 6

or you can replace count(*) by * from the query and it should give you the correct result

You are missing something.

If you do my_sql_num_rows($result) it will display only the total returned rows and, in this logic, the result of only one row is totally correct ( count(*) will return only a row).

If you want to keep that logic, don't use count into your sql query, just

SELECT * from uniqueviews where hour = 13

or if you want to keep that sql query, change the way of fetching result:

mysql_fetch_array($result, MYSQL_NUM);

Try this:

$sql    = "select Count(*) AS COUNT from uniqueviews where hour=13";
$result = mysql_query($sql); 
$count  = mysql_fetch_object($result);

echo $count->COUNT;
"select Count(*) from uniqueviews where hour=13"

This will only return the count ie only a single row ..

If you want to see the result of query ie total count

then do some thing like this

$sql="select Count(*) from uniqueviews where hour=13"; 
$result=mysql_query($sql);
$ml=mysql_fetch_array($result); 
echo $ml[0];

Num rows returned by your query is always 1, because there is only 1 record returned - record with count of rows from that select.

Replace:

$sql="select Count(*) from uniqueviews where hour=13";

With:

$sql="select id from uniqueviews where hour=13";

Then use mysql_num_rows.

But you should get value returned by count(), mysql_num_rows is not for operations like this.

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