can some 1 give me code (very simple) on how to make a counter?

basically, i want when the user loads hte page, it adds one to a variable in my database and then prints it. very simple, no user verification cookies or anything.

please explain the code as well

TIA

    No one is going to just write the code for you. What have you tried?

      mysql_connect("localhost","root","******");
      mysql_select_db("dbtest");
      $query = "UPDATE test SET count = count + 1;
      mysql_query($query);

      im not even sure if that's correct.

      i have a table named "test" in a database called dbtest of course, and i have one field, count which is a small int. im not even sure if that is how it works.

      i don't know how to retrieve the value to check it, or to even see if 1 was added to the count or not

        That'll work, only a couple things:
        1) count is the name of a sql function. I wouldn't use this as a field name.

        2) smallint will only go up to (I think) 127. It won't count beyone this, so you should use either a regular int or a bigint...

          how would i retrieve the value in the variable, which i will change from count to some other name?

          would it be

          mysql_query(SELECT fieldname from table name);

          ??

            Yep, with quotes though. You seem to have at least an idea of what to do, you should test this stuff before posting. Might be easier than you think....

              ok, i jus tried this:

              mysql_connect("localhost","root","****");
              mysql_select_db("whit");
              $query = "UPDATE test SET hits=hits+1";
              mysql_query($query);
              $count = mysql_query("SELECT hits from test");
              echo $count;

              and it didn't work. it printed "Resource id #3" to the page...and i'm not even sure what that means. any suggestions?

                Yeah, the query creates a resource to run, hence the resource id. You need to detch the results to see them. Use mysql_fetch_row or mysql_fetch_array or mysql_fetch_object, etc.

                  oh...so using mysql_fetch_row() what do i put for the argument? "resource id#3"?

                    $count = mysql_query("SELECT hits from test"); 
                    $row = mysql_fetch_array($count);
                    echo $row['hits'];
                    

                      ok!!! thanks it works!!!

                      one last thing...

                      can you explain that code. i mean, i realize that you have to fetch the data, but let's say i wanted to use mysql_fetch_row() what do i have to change, because fetching the array gives me an array, how do i handle the "row"?...

                        They all give you arrays. mysql_fetch_row just fetched a numeric array instead. So if you had used that, you would have echoes $row[0]

                          awesome, thanx so much lordshyrku!!!

                          'n thanx for dealin with a newbie

                            5 months later

                            okk, got some problems. i switched hosts. and tried to do the counter, and its not working.

                            here's the code:

                            <?
                            $host = "*";
                            $user = "
                            ";
                            $db = "neoporta_counter";
                            $pass = "
                            ***";

                            mysql_connect($host,$user,$pass);
                            mysql_select_db($db);

                            mysql_query("update hits set hits = hits + 1");

                            $result = mysql_query("SELECT hits from test");

                            $row = mysql_fetch_array($result);
                            echo $row['hits'];

                            echo $hits;
                            ?>

                            and when i run the page, hoping it will print 1 or something to the page, i get this 'warning':

                            Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/neoporta/public_html/counter.php on line 13

                            can anyone help me?

                              Write a Reply...