If I understand your question correctly, a solution is to use php on the server side to not only retrieve the data but iterate through the record set to build the HTML. Then return the entire HTML (now a string) to the caller.
Depending upon the nuance of your circumstances, you need know nothing about how many rows are coming back, except, of course, a (server side) handler for an empty record set.
With a bit more to it (further capabilities, such as paging and excel dump), I have a generic php interface that retrieves the record set and column names, and therewith builds a table. It uses metadata from the php-SQL call to get the field names.
Something like this crude example: (where record set array is $data)
$rval = '<table class='your styles'>
foreach($data as $d) {
$rval .= '<tr>';
$rval .= "<td>{$d[0]}</td>";
$rval .= "<td>{$d[1]}</td>";
. . . (etc) . . .
$rval .= '</tr>';
} // foreach($data as $d)
$rval .= '</table>';
return $rval;