 <p>I am assuming you are doing this in PHP. It may not be elegant, but it gets it in one query. I think you want to display the table columns as well as the data in one query.</p> <pre><code>&lt;?php $sql = "SELECT * FROM $tablename"; $res = mysql_query($sql); $firstpass = TRUE; while($row = mysql_fetch_assoc($res)){ if($firstpass){ foreach($row as $key =&gt; $value) { //store all the column names here ($key) $firstpass = FALSE; } } //Collect all the column information down here. } ?&gt; </code></pre>