Query Problem-Question

Discussion in 'PHP' started by krdzal, Feb 22, 2009.

  1. #1
    One one page i have a mysql query where it counts the hits of the page. Something like this
    UPDATE SET table_name hits=hits+1 WHERE.......

    The problem that i have is that sometimes it does not increase by 1, but more, like 2 or 3. .When the query is executing? Because u have made on the top of the page the UPDATE query and on the bottom of the page SELECT (echo the result) to see the value of the counter. If it is 0 when i load the page the echo shows 0, after the reloading the page again it is showing the prevous UPDATE ..... Sometimes is OK but sometimes is not
    Any solution???
     
    krdzal, Feb 22, 2009 IP
  2. NFreak

    NFreak Peon

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Well you could always add one to the echo line at the bottom of the page if it is that much of a concern to have the value offset by one. As for the counter increasing by two or sometimes three, all I could think of is that maybe you have your update statement being run multiple times by accident. Maybe you are calling your header sometimes twice?

    I remember there was a time before I used MySQL that I would store all things in text files and use fwrite() and fread(), and my hit counter would glitch if two people viewed the page at "the same time". MySQL doesn't have those issues, as queries are handled one at a time. I hope you can fix your problem, buddy. :)
     
    NFreak, Feb 22, 2009 IP
  3. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #3
    A few question and a couple of comments.

    Are you using the same variable name at the top and the bottom?
    Not the best way.

    Are you unsetting the variable after the echo?


    Are you initialing the variable to 0 before the the sql query?

    By chance is the same variable name used on any other page?
    I think register_gloabals wound need to be on for this to affect anything.


    And then there is the doble loop already mentioned.

    Making your counter a funtion or seperate script and including it once on each page might also solve your pronlem using the syntax include_once() or require_once().
     
    Colbyt, Feb 22, 2009 IP
  4. krdzal

    krdzal Peon

    Messages:
    105
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    i m not using variables i m using this at the top of the page

    $queryup =("UPDATE game SET hits=hits+1,whits=whits+1,thits=thits+1,hhits=hhits+1 WHERE id='".$game_id."'");
    $resultup = mysql_query ($queryup);

    At the bottom i m using SELECT*.... to display how much plays has the game. and it counts sometimes by 1 sometimes by 2 or maybe 3 instead 1 (hits+1)

    It is not echo($variable) the problem. In the DB goes wrong beacause of the UPDATE query, but u dont know why more then 1

    And now i have noticed it is doing only in Mozilla not in IE. I guess is not the query but something else
     
    krdzal, Feb 22, 2009 IP
  5. NFreak

    NFreak Peon

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The Internet browser won't affect the source code, but only how the source code is interpreted. That is very strange. I still think that somehow your update query is being executed multiple times.
     
    NFreak, Feb 22, 2009 IP
  6. krdzal

    krdzal Peon

    Messages:
    105
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You are right, but why? :)
     
    krdzal, Feb 22, 2009 IP