Forum Moderators: coopster
$fbtodaysdate = DATE('Ymd');
$todayhitscount=mysql_query("SELECT TodayHits FROM fb_hitcounter WHERE Date='$fbtodaysdate'");
while($rows=mysql_fetch_array($todayhitscount))
{
if (!$todayhitscount) {
$todayhits = 0;
}
$todayhits=$rows["TodayHits"];
}
if ($todayhits > 0)
{
$todayhits == $todayhits++;
mysql_query ("UPDATE fb_hitcounter SET TodayHits='$todayhits' WHERE Date='$fbtodaysdate' LIMIT 1");
}
if ( $todayhits < 1 ) {
mysql_query ("INSERT INTO fb_hitcounter (TodayHits, Date) VALUES ('$todayhits', '$fbtodaysdate')");
}
while($rows=mysql_fetch_array($todayhitscount))
{
if (!$todayhitscount) {
try changing to:
$numRows = mysql_num_rows($todayhitscount);
if ($numRows >= 1)
{
// Only need first row from the query, since you know the database should only return one result.
$row = mysql_fetch_array($todayhitscount);
$todayhits = $row['TodayHits'];
} else {
$todayhits = 0;
}
Best to test whether you have data first, and then act on it. The mysql_num_rows() function returns the number of rows affected by a query, so in your case if it has one row or higher, the code will execute and use mysql_fetch_array() to fetch the first row in the query (if you use it consecutively, or in a loop, it will continue until it returns each row).
It'll probably be a little easier to read in future if you limit the query to 1 result, that way it there are ever 2 rows with the same date you'll only ever return 1.