Does the query in the nested loop, i.e. '... FROM table2 ...' really contain no WHERE clause? If so, you select all data from table2 for each and every row in table1. If this is really what you want, you should just select table2 data once, put it in an array and loop over this instead.
But, if you on the other hand DO have a WHERE clause in the second query looking something like
SELECT some, fields FROM table2 WHERE some_id = $some_id
then you can do this with one query
$q = 'SELECT some, fields FROM table1 INNER JOIN table2 ON table1.id = table2.corresponding_id_field';
It's possible you should use LEFT JOIN instead of INNER JOIN, since the outer joins lets you retrieve all rows from table1, wether they have any related rows in table2 or not. Table2 fields gets padded with null values.
But if you only wish to retrieve rows from table1 that actually have at least one corresponding row in table2, then you use an inner join.