I have a PHP script that runs indefinitely, performing a specific task every 5-10 seconds (a do-while loop, checks the database at the end of every iteration to determine whether or not it should continue). This task includes MySQL database queries. What is the best way to handle the database connection? Should I:
a.) disconnect and then reconnect to the database every iteration?
b.) set the connection timeout to an indefinite limit?
c.) ping the database to make sure I'm still connected, and reconnect if necessary before executing and queries?
d.) Something else?
EDIT: To clarify, the script sends a push notification to users' iPhones.
IMAPlibrary to check for new emails, and then sending push notifications if new emails exist. I need to access the database to update user data after each iteration. I feel like this approach isn't the most efficient - can you suggest alternatives? Either way, the script needs to run for simultaneously for multiple users, and it needs to run at these fairly quick intervals. Doesn't that more or less rule out using a cron job?