Page 1 of 1

calling php function

Posted: Wed Mar 31, 2004 5:16 pm
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.

Posted: Wed Mar 31, 2004 5:17 pm
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.

Posted: Wed Mar 31, 2004 5:18 pm
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?

like...

Posted: Wed Mar 31, 2004 6:24 pm
by codebuzz
have a javascript that opens a php page and that page sends back data to the original page for processing?????

Posted: Wed Mar 31, 2004 6:27 pm
by andre_c
... but then the php page that was opened with javascript would have to be refreshed constantly.

Posted: Wed Mar 31, 2004 10:25 pm
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

Posted: Wed Mar 31, 2004 10:55 pm
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...

Posted: Thu Apr 01, 2004 6:19 am
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.

Posted: Thu Apr 01, 2004 6:33 am
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?

Posted: Thu Apr 01, 2004 8:54 am
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.

Posted: Thu Apr 01, 2004 8:55 am
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

Posted: Thu Apr 01, 2004 8:58 am
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'

Posted: Thu Apr 01, 2004 9:01 am
by patrikG
Very nice idea, Markl999! Wasn't aware of that feature! Very handy to know!