Page 1 of 1

MySql php pageview counter

Posted: Fri Nov 27, 2009 8:24 pm
by aalbright

Code: Select all

 
<?php
$con = mysql_connect("xxxx","xxxx","xxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
 
$dbname = 'xxxx';
mysql_select_db($dbname);
 
$result = mysql_query("SELECT * FROM prizes") 
or die(mysql_error());  
 
echo "<table border='1'>";
echo "<tr> <th>Prize</th> <th>Views</th> </tr>";
while($row = mysql_fetch_array( $result )) {
    echo "<tr><td>"; 
    echo $row['prize'];
    echo "</td><td>"; 
    echo $row['viewers'];
    echo "</td></tr>"; 
 
echo "</table>";
 
$row['viewers'] = $viewers;
 
$newviewers = $viewers + 1;
 
mysql_query("UPDATE prizes SET viewers = $newviewers
WHERE prize = 'testprize'");
 
mysql_close($con);
 
}
 
?>
I am creating a page that will automatically add 1 to the "Viewers" column of a MySql table each time the page is loaded. For some reason, with the above script, 1 is not added to "viewers" each time the page is loaded. Could somebody please help me with this?

Thank you.

You can see it in action here:
http://bit.ly/81ztLp

Re: MySql php pageview counter

Posted: Fri Nov 27, 2009 8:31 pm
by daedalus__
did you check if the query successful?

or if the query was correct?

what does $newviewers equal?

and why

Code: Select all

 
$row['viewers'] = $viewers;  
$newviewers = $viewers + 1;
 
rather than

Code: Select all

 
$newviewers = $row['viewers'] + 1;