As mentioned, Join is the way to go.
Also I hate seeing people bind their data this way with MySQL. It just means trouble if you suddenly decide to mess with the SQL statement.
checkout fetchrow_hashref. This is your friend.
--
Even smart people are dumb in most things...