unknown columns, column names, and rows into HTML table
<p>I'm trying to display all the information in a msSQL table in an HTML table and have up with something that is not so great.</p>

<pre><code>&lt;table border="1"&gt;
&lt;?
echo "&lt;tr&gt;";
for ($i = 0; $i &lt; mssql_num_fields($result); ++$i){
    echo "&lt;th&gt;" .$column_names[$i] . "&lt;/th&gt;";
}
echo "&lt;/tr&gt;";

$num_rows =  mssql_num_rows($result);

for ($i = 0; $i &lt; $num_rows; ++$i){
    echo "&lt;tr&gt;";
    foreach ($column_names as $key =&gt; $val){    
        $result_row = mssql_query("SELECT * FROM username WHERE id = '$i'");
        $row = mssql_fetch_assoc($result_row);
        echo "&lt;td&gt;";
        echo $row[$val];
        echo "&lt;/td&gt;";
    }
    echo "&lt;/td&gt;";
}
?&gt;
&lt;/table&gt;
</code></pre>

<p>This works. The first part prints out the column names successfully, but as for the rest:
1) I think it is sort of cumbersome to make a query for every time through the loop
2) it doesn't really work because the ids of the rows go much higher than the number of rows in the table, as some ids aren't used.</p>

<p>It seems like I should be able just make one query and pull everything from the database at one go, then build my HTML table from that, but I can't figure out how to access it row by row where I could go <code>$row[next row][shifted value from $column_names</code>. How can improve this query?</p>
