Reputation: 646
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 ?
Upvotes: 2
Views: 7711
Reputation: 2311
Try this:
$sql = "select Count(*) AS COUNT from uniqueviews where hour=13";
$result = mysql_query($sql);
$count = mysql_fetch_object($result);
echo $count->COUNT;
Upvotes: 0
Reputation: 13931
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.
Upvotes: -1
Reputation: 9403
"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];
Upvotes: 0
Reputation: 29922
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);
Upvotes: 0
Reputation: 816
$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
Upvotes: 0
Reputation: 5320
You are actually printing the number of rows that your query has returned.since you are counting the result .You should use mysql_fetch_array instead to get the result of your query.
Upvotes: 0
Reputation: 1723
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;
Upvotes: 5