itsureboy Posted August 23, 2007 Share Posted August 23, 2007 I have a simple counter that updates a row by 1 each time the page is loaded. However, when its on zero and its first executed it adds one then every other time after that for some reason adds 4 and sometimes something different. I just want it to update by 1 each time its reloaded. Here is the code: <?php include 'dbconnect.php'; $id = $id; $query = "SELECT views FROM table WHERE id = '$id' LIMIT 1"; $result = mysql_query($query); while($row = mysql_fetch_row($result)) { $views = $row[0]; $newviews = 0; } mysql_query("UPDATE table SET views='$newviews' WHERE id = '$id'") ?> Thanks..... Quote Link to comment https://forums.phpfreaks.com/topic/66286-simple-view-count-help/ Share on other sites More sharing options...
Jessica Posted August 23, 2007 Share Posted August 23, 2007 <?php include 'dbconnect.php'; $id = $id; //what? This is repeatedly redundant... mysql_query("UPDATE table SET views=views+1 WHERE id = '$id'"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/66286-simple-view-count-help/#findComment-331570 Share on other sites More sharing options...
itsureboy Posted August 23, 2007 Author Share Posted August 23, 2007 lol.... Was just removing some code for the example and somehow that stayed there. Quote Link to comment https://forums.phpfreaks.com/topic/66286-simple-view-count-help/#findComment-331573 Share on other sites More sharing options...
Psycho Posted August 23, 2007 Share Posted August 23, 2007 What you said and the code you posted are at odds with each other. No where in your code do I see the count being increased by 1. All that I would expect to happen is that the views value should get reset to 0 every time that script is run. Also, you havea LIMIT 1 on your query and then use a while loop to get the records. Why? Quote Link to comment https://forums.phpfreaks.com/topic/66286-simple-view-count-help/#findComment-331575 Share on other sites More sharing options...
itsureboy Posted August 23, 2007 Author Share Posted August 23, 2007 Wow sorry totally wrong code.... here it is: <?php include 'dbconnect.php'; mysql_query("SELECT views FROM table WHERE id = '$id'"); $newviews = $views + 1; mysql_query("UPDATE table SET views='$newviews' WHERE id = '$id'") ?> Quote Link to comment https://forums.phpfreaks.com/topic/66286-simple-view-count-help/#findComment-331578 Share on other sites More sharing options...
Jessica Posted August 23, 2007 Share Posted August 23, 2007 Wow sorry totally wrong code.... here it is: <?php include 'dbconnect.php'; mysql_query("SELECT views FROM table WHERE id = '$id'"); $newviews = $views + 1; mysql_query("UPDATE table SET views='$newviews' WHERE id = '$id'") ?> In that code you never get $views from the query. You're setting it to 1 each time. Use the code I posted, it will work. If it doesn't, we can go from there. Quote Link to comment https://forums.phpfreaks.com/topic/66286-simple-view-count-help/#findComment-331581 Share on other sites More sharing options...
itsureboy Posted August 23, 2007 Author Share Posted August 23, 2007 I tried the code you posted it did the same thing. From 0 added 1 then when reloaded added 4(5) then 4 again (9) and so forth i dont no why. Quote Link to comment https://forums.phpfreaks.com/topic/66286-simple-view-count-help/#findComment-331583 Share on other sites More sharing options...
Jessica Posted August 23, 2007 Share Posted August 23, 2007 Is that the ENTIRE code of the page? Create just a page with the connection to the DB and the code, and see it then. There must be some other code, or the file is being called 4 times each time or something. Quote Link to comment https://forums.phpfreaks.com/topic/66286-simple-view-count-help/#findComment-331584 Share on other sites More sharing options...
vijayfreaks Posted August 23, 2007 Share Posted August 23, 2007 Hi.. use the following code.. <?php include 'dbconnect.php'; $res=mysql_query("SELECT views FROM table WHERE id = '$id'"); $views=mysql_result($res,0); $newviews = $views + 1; mysql_query("UPDATE table SET views='$newviews' WHERE id = '$id'"); $af_row=mysql_affected_rows() ; if($af_row >= 1) { echo "$af_row rows affected"; } ?> Regards, Vijay Quote Link to comment https://forums.phpfreaks.com/topic/66286-simple-view-count-help/#findComment-331619 Share on other sites More sharing options...
Jessica Posted August 23, 2007 Share Posted August 23, 2007 There is no need to make a select and then an update - if you do this on every page, you are taxing your database more than you need to. Quote Link to comment https://forums.phpfreaks.com/topic/66286-simple-view-count-help/#findComment-331921 Share on other sites More sharing options...
itsureboy Posted August 26, 2007 Author Share Posted August 26, 2007 I think its adding more than 1 views because i have anothor query. Is this the reason? How can i avoid it adding more than 1 without having to remove other query? Quote Link to comment https://forums.phpfreaks.com/topic/66286-simple-view-count-help/#findComment-334541 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.