View Full Version : MySQL Callback Function?

01-11-2012, 12:43 PM
Here's the concept of what I mean by a "MySQL Callback Function":

Let's suppose I have a php object OBJ_1 which contains data extracted from rows N_1 and M_1 of tables TBL_A and TBL_B, respectively.

One of the purposes of OBJ is to provide a layer of abstraction between the data and the database - so that when, for example, the database changes, only OBJ needs to change and not the whole of the rest of the code, which makes maintenance of the code SO much easier than having mysql statements sprinkled throughout the code! (which I would call a complete mess).

Anyway, as I have things set up at the moment, every time a new instance of OBJ is created (let's call it OBJ_X), it needs to extract rows N_X and M_X from TBL_A and TBL_B, respectively. This is necessary, because the data in rows N_X and M_X may change at any time (e.g. they may be updated by a separate, asynchronos process).

So, is there a mechanism in standard MySQL for attaching a callback function to rows N_X and M_X of TBL_A and TBL_B, from within a running PHP process? The purpose of the callback function would be to notify OBJ_X that the data in the database has changed in rows N_X and M_X.

Old Pedant
01-11-2012, 08:36 PM
No. MySQL has no callback capability.


But it does have TRIGGERS. So if OBJ_X is represented by data in some table(s), you could use a TRIGGER to have a change in TBL_A and/or TBL_B cause changes to OBJ_X.

If OBJ_X exists only in PHP memory, then I don't see any way to do this.

But I also don't see why it's important. A PHP object only lasts as long as it takes for PHP to create an HTTP Response in answer to an HTTP Request, at which point all memory of everything that happened during the creation of the Response is lost. So if you are building an OBJ_X each time an HTTP Request occurs, simply make sure you build it from the *current* data. And so if, 13 milliseconds into a 20 millisecond PHP page, a change occurs in the DB, *that* page won't see it. But any page that is created after the change will.

Are you somehow keeping an object around between Request/Response pairs? As a global known to all PHP pages? (I know you can do that in other technologies--JSP for example--but I also wonder about the value of doing so in a Web environment.)