Hello!
Having attached the php script that performs the query is just as close to almost nothing. That because usually the problem doesn't only reside in the script, but in the way the tables are built and indexes used. So the only possibly analysis for this kind of situations is on both the code (to see the queries) and the db tables, to see the indexes, data types used etc. So if you can also attach the tables used in the script, that'd be really of great help. Or, if you are willing to allow me ftp + phpmyadmin, I'll see for myself all the information needed. Moreover, a mysql slow query log would be greatly beneficial for use under such circumstances, but usually shared hosting doesn't allow customers to see it; if you have a dedicated server, I can instruct on how to create or fetch it.
This is a placeholder bid, at this moment I can't tell 100% for sure that it only takes 5h, because I think there are several files and queries to be abalized, not only a single one, thus I'll wait for your files for a proper evaluation.
Regards,
Dragos