First, you need to make sure PHP is running as a module in Apache, not in CGI. Then, you need to use mysql_pconnect() when you open your database connection. this will create a persistent connection that will stay open throughout the browser session.
That should solve your problem with the temporary tables.
EDIT > Note that your temporary table may not necessarily be destroyed once each individual USER closes their browser, but when the thread is finished.
another solution you might try is creating a normal table named by the session id. Then implement a session save handler destroy function which drops that table from the database once the session is destroyed. For more information on session save handlers, check:
http://www.zend.com/manual/function.session-set-save-handler.php
I've never tried this method though, but it seems it should work for your situation