Re: COUNT() returns 1, but should be 0

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Lew Mark-Andrews wrote:

I get a count of 1 for the number of visits
($visitcount) using my following code. It should be 0. -------------------
<snipped> code in check_visits.php
$sql = "SELECT COUNT(visitdate) FROM cardlist GROUP BY visitdate";
$query = mysql_query($sql) or die("<br><br><div align='center'> Cannot
query the database.<br><br>" . "MySQL Error No. " . mysql_errno() . " :
<br><br>" . mysql_error() . "<br><br></div>");
$visitcount = mysql_num_rows($query);
[snip]
echo "<p>Total number of users: $usercount</p>";
echo "<p>Total number of visits: $visitcount</p><br>"; // <--- Should be zero.
</snipped>

That's because you're displaying how many "rows" were returned from the query, not the value that COUNT() returns. A query with COUNT() is always going to return at least one row.


$visitcount = mysql_result($query,0);

--
---John Holmes...

Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/

php|architect: The Magazine for PHP Professionals – www.phparch.com



--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux