Hi I am trying to create a page counter in PHP. Table structure: Table Name: tbl_counter Column Name:total_views Column Datatype: integer Code (markup): I am using the following SQL query on the page. UPDATE tbl_counter SET total_views=total_views+1 Code (markup): I know its weird but, sometimes the counter increases twice instead of 1, any idea why?
Should be like: while($rows=mysql_fetch_array($connectiontoyourdatabase)) { $rows['pageviews']++ } Not sure if that will work but give it a try anyways
Lol above that's the worse possible type of script you can get. do this. //MySQL /*The MySQL query to the updater.*/ CREATE TABLE hitlogs ( hits BIGINT( 40 ) NOT NULL ); Code (markup): PHP code <?php //Connect to the DB $connection = mysql_connect("localhost", "user", "pass"); mysql_select_db("database", $connection); //The function to program it. function update_hits($con) { if(!mysql_ping($con)) { return false; } else { mysql_query("UPDATE IF EXISTS hitlogs SET hits=hits+1 LIMIT 1;",$con); if(!mysql_affected_rows($con)) { mysql_query("INSERT INTO hitslogs (hits) VALUES (1);"); } return true; } } //Update the hits update_hits($connection); ?> Code (markup): That's probably the most optimized piece of code you can possibly make, You should look at hit counters - they handle all of the usage for you. You could additionally use files and fwrite with (int) to log it, It's less heavy on MySQL.