#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Norcross, GA
    Posts
    458
    Rep Power
    15
    I have a field in my database that stores the number of times a particular row has been accessed through a certain page in variable $counter (INT).

    When a the page is opened, a query is made based on a previous screen's selection (which displays the requested data). I also have a very simple snippet to run the counter, but it won't count over the number one (default field value is zero (0).

    The code is exceedingly simple, but I don't understand why it isn't working properly:

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>

    $result = mysql_query("SELECT * FROM spec WHERE model='$model'",$db);

    if ($result) {

    $myrow = mysql_fetch_array ($result);

    $counter = $myrow['counter'];
    $counter = counter + 1;

    $updatecounter = "UPDATE spec SET counter = $counter WHERE model = '$model'";

    $result2 = mysql_query($updatecounter,$db);

    }

    [/code]

    Your help is appreciated,
    - Mike
  2. #2
  3. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    19
    The problem might be in this line:

    $counter = counter + 1;

    No $ in front of counter.

    Try using $counter++;
    instead.

    Better yet, use this query:

    mysql_query("update spec set counter=counter+1 where model='$model'");

    Saves you a few steps ;-)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Norcross, GA
    Posts
    458
    Rep Power
    15
    I wondered if I could just put the counter function in the query...

    Could I also use:

    mysql_query(UPDATE spec SET counter = ++$counter WHERE blah = blah);

    Thanks for pointing out the missing $

    - Mike
  6. #4
  7. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Brussels, Belgium
    Posts
    14,647
    Rep Power
    4493
    You actually don't need to pull the counter value out at all. (Along the lines of what rodk said)
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    $result = mysql_query("SELECT * FROM spec WHERE model='$model'",$db);
    if ($row = mysql_fetch_array($result))
    {
    $result2 = mysql_query("UPDATE spec SET count=count+1 WHERE model='$model'",$db);
    }
    [/code]
    You're initial result is still set, so you can still use $row["counter"] or $row["model"] later in the script.

    ---JH

    [This message has been edited by SepodatiCreations (edited September 24, 2000).]

Similar Threads

  1. Replies: 4
    Last Post: January 6th, 2004, 04:56 PM
  2. Hardware (un)support for performance counter.
    By peenie in forum Computer Hardware
    Replies: 0
    Last Post: October 28th, 2003, 01:48 PM
  3. About creating a web page counter
    By kapot in forum Perl Programming
    Replies: 9
    Last Post: October 22nd, 2003, 07:13 PM
  4. PHP hit counter & sessions problem
    By irishairpics in forum PHP Development
    Replies: 11
    Last Post: September 23rd, 2003, 08:00 AM
  5. Uknow Known MYSQLERROR
    By andyw in forum MySQL Help
    Replies: 7
    Last Post: January 23rd, 2001, 10:34 AM

IMN logo majestic logo threadwatch logo seochat tools logo