calling php function

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
codebuzz
Forum Newbie
Posts: 6
Joined: Wed Mar 31, 2004 5:16 pm

calling php function

Post by codebuzz »

OK, got a couple of questions.

I'm developing an app that will query a database and load a default page. I then want to periodically (every minute) be able to check if there have been any updates to the database...WITHOUT refreshing the page.

Is this possible? Can you call a php function from javascript that checks the DB and returns true if any updates have been made?

Thanks for any replies.
Last edited by codebuzz on Wed Mar 31, 2004 5:17 pm, edited 1 time in total.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Not without refreshing the page. As PHP is server side, and Javascript is client side then you have to send another http request to get any new info.
codebuzz
Forum Newbie
Posts: 6
Joined: Wed Mar 31, 2004 5:16 pm

Post by codebuzz »

so ...is there any way to run another php script that can return a value to the main script that tells whether there was an update?
codebuzz
Forum Newbie
Posts: 6
Joined: Wed Mar 31, 2004 5:16 pm

like...

Post by codebuzz »

have a javascript that opens a php page and that page sends back data to the original page for processing?????
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

... but then the php page that was opened with javascript would have to be refreshed constantly.
codebuzz
Forum Newbie
Posts: 6
Joined: Wed Mar 31, 2004 5:16 pm

Post by codebuzz »

well, the javascript can control how often the other PHP page is called.

I don't even know if it is possible....I should just refresh the page, but I'd rather not have the database take a hit like that.

Lets say this app is for about 200 people and if 100 are on and it refreshes every minute...that's a lot hits on the DB.

Any one got suggestions of how I could get around making all these calls to the DB unless there has been an update??

Thanks
Last edited by codebuzz on Thu Apr 01, 2004 9:14 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

write a hidden iframe to do a quick query php file? that script, when db updated, could write some additionaly javascript that tells the parent window to reload. Otherwise the iframed php would just refresh itself whenever you wanted...
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

wether or not you refresh the page you are still going to have to hit the DB to see if its been updated! If you are trying to ease up on your server i could see why you wouldnt want to reload, but if you are trying to ease up on the DB, it doesnt matter, just relaod, but have a file on your machine that something is written to whenever someone does something in the Dbase then just check that file and if it indicates that something has changed then hit the dbase and set the file back to no nochanged state. This still may hamper performance, however, I don't know if it would be more resource intensive to hit the Dbase or to read and write a file all the time.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

depends on the database you're using, you could set it so that the update-event is triggered whenever the database is updated.

Not possible in mySQL, though. AFAIK should be possible in SQLite and Oracle, but have no experience with it.

What kind of application are you coding?
codebuzz
Forum Newbie
Posts: 6
Joined: Wed Mar 31, 2004 5:16 pm

Post by codebuzz »

magicrobotmonkey wrote:wether or not you refresh the page you are still going to have to hit the DB to see if its been updated! If you are trying to ease up on your server i could see why you wouldnt want to reload, but if you are trying to ease up on the DB, it doesnt matter, just relaod, but have a file on your machine that something is written to whenever someone does something in the Dbase then just check that file and if it indicates that something has changed then hit the dbase and set the file back to no nochanged state. This still may hamper performance, however, I don't know if it would be more resource intensive to hit the Dbase or to read and write a file all the time.
That is an excellent idea...Thank you. I think I can use a combo of your idea and the iFrame...

I can have a file that holds a variable with a timestamp value on the server. When a change is made to the DB, it updates the timestamp in that file.... When the user is viewing the page, there is an iFrame that refreshed a php script every minute or so. Every time it loads, it pulls the current timestamp from the file. When it refreshes, it checks the files timestamp and if it is newer, is makes the parent page refresh!

Comments, suggestions please...................Keep em coming and thanks.
codebuzz
Forum Newbie
Posts: 6
Joined: Wed Mar 31, 2004 5:16 pm

Post by codebuzz »

patrikG wrote:depends on the database you're using, you could set it so that the update-event is triggered whenever the database is updated.

Not possible in mySQL, though. AFAIK should be possible in SQLite and Oracle, but have no experience with it.

What kind of application are you coding?
PHP/MySQL - Problem tracking app
Last edited by codebuzz on Thu Apr 01, 2004 9:13 am, edited 1 time in total.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You could use MySQL's Binary logging feature.
See http://www.mysql.com/doc/en/Binary_log.html

"The binary log, like the old update log, only logs statements that really update data. So an UPDATE or a DELETE with a WHERE that finds no rows is not written to the log. It even skips UPDATE statements that set a column to the value it already has."

So by checking the size/date of the binary log file you know if 'somethings changed'
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Very nice idea, Markl999! Wasn't aware of that feature! Very handy to know!
Post Reply