CREATE TEMPORARY TABLE '$username'
I assume you actually meant to say:
"CREATE TEMPORARY TABLE '$username' ...
i.e. the whole thing is inside double-quotes so that the variable is
expanded. If that's so, then I'd recommend looking at the contents
of what you allow a username to be, and see if that conflicts with
MySql's rules for naming table (no, I can't give a worthwhile rendition
here from memory...)