Is it possible?

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
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Is it possible?

Post by Dale »

I'm sorry if this is posted in the wrong room...

Is it possible for a PHP script to update the database every 'x' amount of minutes without someone actually viewing that page?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yes. It's called 'cron' usually.


Moved to PHP - Code.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

not php directly, but you can set up a cron job which will run a specified script every specified time amount. Unless you're on a windows server.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Cron can be done on Windows.. under Scheduled Tasks.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yup.. I have a cron job running on windows to syncronise a remote mysql db with an access one on my local machine...

(Set this up a while ago and since discovered myodbc :( )

Just schedule "php.exe yourscriptname.php" every 5 mins or whatever.
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

Ahhh... i wanted something that will update without any assitance, as i am using a hosting package... so i cannot control 'php.exe'. I might just have it update whenever i click a button.

Thx anyways :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Just a thought but u could bury it into the homepage of your website or whatever so your visitors do the work "blindly" for you.

They don't see what happened on the server when they requested your web page but your db gets updated (of course... you need enough visitors for it to be effective and you'd have to cut out all the errors in case of db failures and users getting 'mysql connect failed... bla bla bla')

Or you can just have a page open that refreshes or redirects to itself very x minutes
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

Ahhh, i've jsut found cronjobs in cPanel... i could use that. However the idea of users doing the updated via an invisible iframe or something sounds much better.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

provided there is no visible output (or you capture it and toss it) you can just include it in the page code like d11wtq was getting at. No need for an iframe, or any "special" trickery for the user's involvement.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

There are pros and cons to putting the updating hidden inside your homepage.

Pro:
The DB only gets updated when it needs to be. There's no sense updating it every 5 minutes if no one's looking at the changes

Con:
Attaching the work to a page load will increase the load time of the page. Depending on how much data there is to crunch, and how busy the server is, this could add up.

One solution may be to just do the work for the first page load of the hour. So, if someone doesn't look at your page until 9:35, that's the page that does the work. Everyone else looking at the page that hour won't have to wait for the DB to be synched.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply