Dynamically Update MYSQL from php

I didn’t really know what to put under summary, let alone which forum to put this in, as it could be under PHP, Javascript, or MYSQL forums.

What I want to do is have a value displayed in a php file which is stored in a MYSQL database. This value needs to be updated every second (by a mathematical operation), then changed in the browser, and updated to the mysql table.

I am open to other ideas other than MYSQL to store this value. Keep in mind, though, that there will be thousands of these values that need to be updated in this manner.

I’m not trying to blow up my server here, so help me optimize this! :slight_smile:

Less abstract, more concrete, please. You’re at risk of getting very bad advice otherwise.

What’s the actual problem you’re trying to solve?

What I need to do is have a text field which holds a number to increase by a specified value every second. The values in this text field need to be the same when the page is closed and reopened again, possibly being stored in a mysql database maybe.

So it’s a clock (in seconds going to infinity) with some start time.

Say you want the number to be 0 at 1/15/2010 00:00 (midnight) and count up from there, once per second. All you have to store is the number 0 and the time it starts, 1/15/2010 00:00.

When someone loads the page at 6PM, you add (6 hours * 60 minutes * 60 seconds) and get 21600, and display that on the page.

When someone loads the page again at 9PM, you add (9 hours * 60 minutes * 60 seconds) and get 32400, and display that on the page.

JavaScript can make the display increment by 1 each second from there, but you’re never changing what’s in the database. Unless this clock isn’t supposed to be updated every second all the time…

But that’s assuming that the rate in which the “clock” increases is always 1 per 1 second, which isn’t want I’m looking for unfortunately.

Does the page update the database? Or the database update the page?

The database will update the text box, which displays the “clock” as he mentioned. I need the value of the text box to update even when the page is not being viewed, or else update to the correct amount the next time the person views the page again.

Then you probably want to poll the server with ajax.

That is, have javascript fetch the value from a php script (which fetches the value from the db) every second, and update the field on the page.

Could you give me some code which would allow me to do this?

Ajax or javascript won’t work when “the page is not being viewed”.

So, OP have to learn how to evaluate estimated value instead of writing it to database constantly.
And real matter of the actual problem is still needed. Dunno why it is still hidden from us.

But that’s assuming that the rate in which the “clock” increases is always 1 per 1 second, which isn’t want I’m looking for unfortunately.

It doesn’t matter. It has formula to evaluate. Just apply it.

You said you’re stored value is achieved by “mathematic calculation”.

Store both your value and a last calculated server timestamp.

When you load the page collect this value and the timestamp from the database. Use the current server time to calculate the difference since the last calculation.


Calculate the new value. Should the value have any reliance on the previous value you can rely on a for loop here, otherwise you can rely on simple calculation and multiply it by your difference.

Update the database with your new value and your new timestamp (the current time, not the difference). Ensure you store the time at which your difference was calculated from. Otherwise, if your calculation took three seconds for example during a server lag or something, your future counts will be inaccurate.

Output your value to the browser however and wherever you need it.

Use javascript to get the value to increase on the browser side. Nothing needs to be sent to the server as next page load the server can calculate this value itself!

EDIT: This’ll seriously reduce database usage and bandwidth. Anything not completely clear or your not sure about just reply =]

Thank You everyone, I seem to have gotten all that I have asked for. :slight_smile: