You should be able to create tables directly using php:
<php>
echo "<table>\n";
for ( $counter = 1; $counter <= 3; $counter += 1) {
echo "<tr><td>data $counter</td><td>data_2 $counter</td></tr>\n";
}
echo "</table>";
</php>
You must be collecting the sql data in rows, so parse it as needed and feed it into the table. Or am I missing something?