Quantcast

Forums | MacLife

You are not logged in.

#1 2007-09-27 5:06 pm

Jasoco
Your own personal Jesus
From: Doylestown, PA, USA, Earth
Registered: 2000-08-26
Posts: 8848
Website

I thought a simple counter would be easy to make.. I was wrong.

Wouldn't it be simple? I mean you have a table in a database that gets read and updated every time a page is loaded.

But I am having a hell of a time with one I am just trying to create for feedback.

This is the jist of the code: (Not the real table name, etc.)

Code:

<?php
    $database = "jasoco_table1";
    
    $dbx = mysql_connect("localhost","******","*****");
    
    @mysql_select_db($database) or die( "Unable to select database");

    $query="SELECT * FROM options WHERE options.name='home_count'";
    
    $result=mysql_query($query);
    
    $count=mysql_result($result,'0',"value");
    mysql_close();    
?>

The top of the page. It would read the current value into a variable. $count

The bottom of the page would be the adding and saving back:

Code:

<?php
    $database = "jasoco_table1";
    
    $dbx = mysql_connect("localhost","******","******");

    echo "Hit Counter: ".$count;
    
    @mysql_select_db($database) or die( "Unable to select database");

    $count = $count + 1;
    $query = "UPDATE options SET options.value = '".$count."' WHERE name = 'home_count'";

    mysql_query($query);

    mysql_close();

?>

But what happens is that the variable adds too much most of the time. Now, I know there isn't anyone really visiting the page except me, so it should in theory only go up one per reload.. but instead it jumps 2 or 4 or more.

Why would it be repeating? My code seems straightforward. Load count. Add one. Print number. Save new number.


                         Haikus are easy
          But sometimes they don't make sense
                           Refrigerator

                Jasoco.netGeekPub Forums

Offline

 

#2 2007-09-27 8:01 pm

Gipetto
Yankee Doodle's noodle
Royal Wombat
From: People! Ahg!!
Registered: 2000-09-24
Posts: 9941
Website

Re: I thought a simple counter would be easy to make.. I was wrong.

Are you using PHP5 with mysqli?
If so, I have a suggestion.

Offline

 

#3 2007-09-27 10:32 pm

Jasoco
Your own personal Jesus
From: Doylestown, PA, USA, Earth
Registered: 2000-08-26
Posts: 8848
Website

Re: I thought a simple counter would be easy to make.. I was wrong.

General server information:
Operating system    Linux
Kernel version    2.6.9-42.ELsmp
Apache version    1.3.37 (Unix)
PHP version    4.4.5
MySQL version    4.1.21-standard

Is it something simple? Stupid? Am I completely braindead? I am just learning this stuff. But I thought reading, adding and saving back would be straightforward.

Last edited by Jasoco (2007-09-27 10:33 pm)


                         Haikus are easy
          But sometimes they don't make sense
                           Refrigerator

                Jasoco.netGeekPub Forums

Offline

 

#4 2007-09-28 1:01 am

Gipetto
Yankee Doodle's noodle
Royal Wombat
From: People! Ahg!!
Registered: 2000-09-24
Posts: 9941
Website

Re: I thought a simple counter would be easy to make.. I was wrong.

Bummer, you're on PHP4 so my trick won't work.
It is a simple operation and your code looks right, but you should be doing it all in the same connection, something like this:

Code:

<?php
    $database = "jasoco_table1";
    $dbx = mysql_connect("localhost","******","*****");
    @mysql_select_db($database) or die( "Unable to select database");
    
    // get current value
    $query="SELECT * FROM options WHERE options.name='home_count'";
    $result=mysql_query($query);
    $count=mysql_result($result,'0',"value");
    
    // update and insert
    $count++;
    $query = "UPDATE options SET options.value = '".$count."' WHERE name = 'home_count'";
    mysql_query($query);

    mysql_close();    
?>

Then, later in the page:

<p>Hit count: <?php echo $count; ?></p>

I suspect the problem with your page is that you could be incrementing the hit_count twice in the page?

At least like this you get two benefits:
1. all your sql happens on the same connection
2. you select, increment and save right away

Try to do all your SQL on the same connection - if you want, you can open the connection at the top of the page, close it at the bottom, and then use mysql_free_result() in between queries. If you're opening and closing connections throughout the page you're putting undue stress on the server by increasing both memory allocation and db server connections...

If you're using includes and that is why you're opening and closing so much, then consider gathering all mysql data first thing and then declaring the variables in the global scope, its may feel dirty, but it's not too bad.

Offline

 

#5 2007-09-28 4:32 am

Alien
Forum Czar
Administrator
From: Republic of Amsterdam
Registered: 1999-07-05
Posts: 16942
Website

Re: I thought a simple counter would be easy to make.. I was wrong.

select * is bad form.

Just sayin'. tongue

,xtG
.tsooJ


http://macstack.net/forums/images/smilies/lol.gif

Offline

 

#6 2007-09-28 6:51 am

Daniel
[dp] design#
From: Melbourne, FL
Registered: 2000-11-21
Posts: 9706
Website

Re: I thought a simple counter would be easy to make.. I was wrong.

Indeed.

SELECT value FROM options WHERE name='home_count' LIMIT 1;

Would be better.

Last edited by Daniel (2007-09-28 6:52 am)


Airman Dan
Private Pilot, Instrument Airplane Single-Engine Land
http://homepage.mac.com/dp.design/.Pictures/atat/AtAT-Banner.jpg

Offline

 

#7 2007-09-28 8:00 am

Alien
Forum Czar
Administrator
From: Republic of Amsterdam
Registered: 1999-07-05
Posts: 16942
Website

Re: I thought a simple counter would be easy to make.. I was wrong.

And putting SQL keywords in all-caps is fugly.

,xtG
.tsooJ


http://macstack.net/forums/images/smilies/lol.gif

Offline

 

#8 2007-09-28 8:27 am

Daniel
[dp] design#
From: Melbourne, FL
Registered: 2000-11-21
Posts: 9706
Website

Re: I thought a simple counter would be easy to make.. I was wrong.

tongue

I do it for readability.


Airman Dan
Private Pilot, Instrument Airplane Single-Engine Land
http://homepage.mac.com/dp.design/.Pictures/atat/AtAT-Banner.jpg

Offline

 

#9 2007-09-28 1:02 pm

TonyPrevite
Slobbering Jester
Royal Wombat
From: Glendale, AZ
Registered: 2002-04-14
Posts: 3606
Website

Re: I thought a simple counter would be easy to make.. I was wrong.

Alien wrote:

And putting SQL keywords in all-caps is fugly.

,xtG
.tsooJ

Opinions are like bungholes you know wink   I like it for readability as well, so there tongue

Offline

 

#10 2007-09-28 4:30 pm

Gipetto
Yankee Doodle's noodle
Royal Wombat
From: People! Ahg!!
Registered: 2000-09-24
Posts: 9941
Website

Re: I thought a simple counter would be easy to make.. I was wrong.

Alien wrote:

select * is bad form.

Just sayin'. tongue

,xtG
.tsooJ

Well, in his defense, with such a small data set its probably not any kind of extra burden.

Pulling 10000 rows would be a little different.

Offline

 

#11 2007-09-29 6:10 am

Alien
Forum Czar
Administrator
From: Republic of Amsterdam
Registered: 1999-07-05
Posts: 16942
Website

Re: I thought a simple counter would be easy to make.. I was wrong.

TonyPrevite wrote:

Alien wrote:

And putting SQL keywords in all-caps is fugly.

Opinions are like bungholes you know wink   I like it for readability as well, so there tongue

Trying to get into the details seems to be a religious issue — nearly everybody is convinced that every style but their own is ugly and unreadable. Leave out the "but their own" and they're probably right…
— Jerry Coffin


http://macstack.net/forums/images/smilies/lol.gif

Offline

 

#12 2007-09-30 4:34 pm

Jasoco
Your own personal Jesus
From: Doylestown, PA, USA, Earth
Registered: 2000-08-26
Posts: 8848
Website

Re: I thought a simple counter would be easy to make.. I was wrong.

Yeah, it's PHP 4. I don't know why FourBucks hasn't updated. They still offer WordPress 1.5 when the official version is up to 2.0+.

I gotta contact them. See what's so behind.


                         Haikus are easy
          But sometimes they don't make sense
                           Refrigerator

                Jasoco.netGeekPub Forums

Offline

 

Board footer

Powered by PunBB 1.2.6
© Copyright 2002–2005 Rickard Andersson