Tutorial :Can php listen for a mysql database change or update? …thinking real-time, live search here



Question:

Im trying to think of the best way to handle real time search for a website. The only solution I can come up with is to keep checking the server every few seconds for something new in the database, but that does not seem very practical to me.


Solution:1

As of 5.0.2 MySQL supports triggers: http://dev.mysql.com/doc/refman/5.0/en/triggers.html

But unfortunately I don't know MySQL well enough to know whether you could do anything "useful" in them to notify the app (in Oracle I'd be thinking xp_'s). At very least you could write a row into a single, audit-like table so that you only have to poll in one place.


Solution:2

No, it cannot, because MySQL cannot. You need to use other technologies, possibly including a message queue, or a streaming database.


Solution:3

I have an app I worked on 5-6 years ago now. I was looking for a similar solution at the time. It was for my pre-twitter application - my own development of sms to web page with no refresh. I had a way I was polling at the time ...but at least I didnt have any hideous page refresh. Plus ...I don

That led me to comet server. That was my answer.

I was led to believe that gmail chat used true push technology as well as opposed to polling. Hope that leads you in the right direction.


Solution:4

AFAIK, there's no way to tell MySQL to ping your script when something happens there.

Polling seems to be the only plausible solution...


Solution:5

I have not played with it but have noticed that the Symfony framework has introduced "event notification"... basically, every time one part of the app makes an INSERT or UPDATE, it creates an event, and other pieces of code can register to "listen" to events and fire.

That would be all at the PHP level, not the database level, but is I think what you're looking for.

There's a good article about symfony events here: http://www.symfony-project.org/blog/2009/02/21/using-the-symfony-event-system


Solution:6

You could use something like Gearman (which has a MySQL interface) to prod your php code when something new happens. Maybe you could use those events to build a static page with all the newest info, and use polling on the client side to grab that static page.

I've yet to use Gearman at all, though, so the details on such a setup are beyond me.


Solution:7

even if php could how would u show the updates to the user, u will have to query the server to show the results.

and the only way is what you are already thinking of, querying the server every 5 or more seconds,.,,.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »