schoolboy, thanks for the suggestion and for the example; I'll give this a try.
In the meantime, I've moved my php file to /includes/functions/extra_functions and now it runs as soon as I load the home page but at least it can now use $db to run the database query. I thought I could now just add a notifier so it only runs on demand but I'm not sure how to do that so I'm looking at the example in the wiki. If you, or anyone else, have any suggestions on that, please let me know.
Your idea seems easier to implement, though, so I'll try it first. Thanks again.