Periodically Storing PHP Variable (newbish question)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Tomcat7194
Forum Commoner
Posts: 48
Joined: Mon Jul 31, 2006 1:34 pm

Periodically Storing PHP Variable (newbish question)

Post by Tomcat7194 »

Hello.

I'm just getting started with PHP, and I have a question that probably has a simple answer, but extends beyond my knowledge. I'm working with a PHP script that generates an integer variable based on dynamically changing factors. I want to keep a record of this variable in a MySQL document, so I can chart its changes over time.

I'm assuming that to keep a record of a PHP variable, I'm going to need some kind of server side script that periodically calls my PHP script, processes the variable, and then stores it in MySQL. What language should I use? Where do I put the script on my server? Is there a good tutorial I should read so I can learn how to do this kind of stuff?

Thanks

Tom
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

This should be a very straight forward and easy task. The thing to remember is that every time that var has activity it needs to be documented in the database, so you are going to need to develop a means to account for the var, make it available, update it and process it. It is not hard to do, but there is some logic involved in it.
Tomcat7194
Forum Commoner
Posts: 48
Joined: Mon Jul 31, 2006 1:34 pm

Post by Tomcat7194 »

I figured I'll use time as the trigger for storing the variable, so that it adds a new entry to the database every 5 minutes or so.

My issue is how to actually go about doing this--can I do something like that with PHP? It seems like I would then have to have the script running all the time, so it would always be monitoring and storing the variable. In order to do this, wouldn't I have to do something server side (or leave a browser open all the time with the script running)? Or can PHP handle that sort of thing on its own?

Tom
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Can you set up a cron job?
Tomcat7194
Forum Commoner
Posts: 48
Joined: Mon Jul 31, 2006 1:34 pm

Post by Tomcat7194 »

Yes, my hosting company supports Cron, but I have never used it, and I have no idea how to use it. Is there a simple way to set one up (simple enough that someone could explain it here) or is there a good tutorial you can refer me to?
Thanks
Tom
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Every now and again you can set up a cron job with your cpanel. Have a look around your control panel and see if there is something in there that can walk you through it. I think basically you tell your server what time to do what and point it at a script and the server will execute it. I think that it how it should work.
Tomcat7194
Forum Commoner
Posts: 48
Joined: Mon Jul 31, 2006 1:34 pm

Post by Tomcat7194 »

Yeah, I found a Crontab function in Cpanel, and I figured out how to make it send a command at a specified time interval. For the command, I just entered the URL of the script, but that doesn't appear to be working. Do I have to enter some kind of command besides just the URL to get it to run the script?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I don't think you do, but I am not that certain (I have never had a use for cron, so I am a newbie myself with it). Is there anything telling you how it works in the cpanel?
Tomcat7194
Forum Commoner
Posts: 48
Joined: Mon Jul 31, 2006 1:34 pm

Post by Tomcat7194 »

I got it working. It turns out that I had not selected the right options for the Cron Job.
Thanks for suggesting the cron method--it appears do the job very nicely.
Tom
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Glad I could help.
Post Reply