Page 1 of 1
Periodically Storing PHP Variable (newbish question)
Posted: Wed Aug 02, 2006 12:55 pm
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
Posted: Wed Aug 02, 2006 12:58 pm
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.
Posted: Wed Aug 02, 2006 1:13 pm
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
Posted: Wed Aug 02, 2006 7:41 pm
by RobertGonzalez
Can you set up a cron job?
Posted: Mon Aug 07, 2006 2:30 pm
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
Posted: Mon Aug 07, 2006 2:50 pm
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.
Posted: Mon Aug 07, 2006 2:56 pm
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?
Posted: Mon Aug 07, 2006 3:18 pm
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?
Posted: Mon Aug 07, 2006 4:02 pm
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
Posted: Mon Aug 07, 2006 4:03 pm
by RobertGonzalez
Glad I could help.