简体   繁体   English

如何打印mysql_num_rows?

[英]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 并试图打印行数,但只得到1

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 根据查询,它应该打印6但其打印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. Count(*)仅返回一行,其中包含行数。 That's why you get only 1 as return value. 这就是为什么您只获得1作为返回值的原因。 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 或者您可以在查询中用*替换count(*) ,它应该为您提供正确的结果

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). 如果执行my_sql_num_rows($result) ,它将仅显示返回的总行 ,按照这种逻辑,只有一行的结果是完全正确的( count(*)仅返回一行)。

If you want to keep that logic, don't use count into your sql query, just 如果您想保持这种逻辑,请不要在SQL查询中使用count

SELECT * from uniqueviews where hour = 13

or if you want to keep that sql query, change the way of fetching result: 或者,如果您想保留该sql查询,请更改获取结果的方式:

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. 您的查询返回的行数始终为1,因为仅返回1条记录-记录中包含该选择的行数。

Replace: 更换:

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

With: 附:

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

Then use mysql_num_rows. 然后使用mysql_num_rows。

But you should get value returned by count(), mysql_num_rows is not for operations like this. 但是您应该获得由count()返回的值,mysql_num_rows不适用于此类操作。

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

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