Jump to content


Increment one in MySQL

  • Please log in to reply
1 reply to this topic

#1 moret

  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 01 September 2006 - 11:12 AM

Hi all,

I'm doing a forum, and I have a table field named 'hits'. I want to increment one the value of this field each time a user visits a topic. It seems easy, but when page executes:

$result=mysql_query("UPDATE forum_messages SET hits=hits+1 WHERE id='$id");

MySQL increments 2 instead 1, why?


#2 Jenk

  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 01 September 2006 - 11:37 AM

for pure semantics change your query to:


$result=mysql_query("UPDATE `forum_messages` SET `hits` = `hits` + 1 WHERE `id` ='$id'");

(also note you were missing a closing apostrophe after $id)

now check that you don't run the query twice in the process of your application logic.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users