First to answer your question, yes MySQL does use some caching to make lookups faster. My example site was more pointing to how fast queries should run even without caching, but either way, I think you got the point.
Secondly, I would not doubt that your code is to blame, but I would doubt that it is the specific 2 lines that you had originally presented. From what I can tell, there should be no problem in issuing those two commands.
Something to try (general coding practice really) is to remove all the fluff, and make a direct query, starting with ALL links, and moving on to a category that you know the name of... AKA - remove the dynamic stuff to run a test.
If there is something "clogging" your MySQL application, its probably not queries, but lingering connections. Check to make sure that you dont accidentally loop through the mysql_connect() function. Also, if running many non-persistent connections, remember to use the mysql_close() function.
As for "guides" on how to do this all, you are left to the mercy of Google for anything complicated. Although I regularly reference
The W3School's PHP Tutorial and another friendly DIY site
tizag but neither of these compare to the completeness of the
PHP/MySQL Function Docs
Hope some/any of this helps.