Jump to content

Simple View Count Help


itsureboy

Recommended Posts

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.....

Link to comment
https://forums.phpfreaks.com/topic/66286-simple-view-count-help/
Share on other sites

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?

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.