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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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