Try http://dev.mysql.com/doc/mysql/en/mysql_fetch_fields.html to get the names and data types of your result set, or http://dev.mysql.com/doc/mysql/en/mysql_list_fields.html which returns the field names of a table. Which you use depends on whether you are selecting from multiple tables or not. You can also use use SHOW COLUMNS FROM tbl_name, but the results are more limited.
As to your sort options, you need an html link in your column headings that return the query you will use for sorting. Have a look at how phpmysqladmin does it for a very ellagent solution.
//copied from phpmysqladmin
<!-- Results table headers -->
<tr>
<td colspan="2"></td>
<th>
<a href="sql.php?lang=en&server=1&db=webtrack&table=users&pos=0&session_max_rows=30&disp_direction=horizontal&repeat_cells=100&dontlimitchars=&sql_query=SELECT+%2A+FROM+%60users%60+ORDER+BY+%60id%60+ASC">
id</a>
</th>
<th>
<a href="sql.php?lang=en&server=1&db=webtrack&table=users&pos=0&session_max_rows=30&disp_direction=horizontal&repeat_cells=100&dontlimitchars=&sql_query=SELECT+%2A+FROM+%60users%60+ORDER+BY+%60client%60+ASC">
client</a>
</th>
<th>
<a href="sql.php?lang=en&server=1&db=webtrack&table=users&pos=0&session_max_rows=30&disp_direction=horizontal&repeat_cells=100&dontlimitchars=&sql_query=SELECT+%2A+FROM+%60users%60+ORDER+BY+%60usr%60+ASC">
usr</a>
</th>
<th>
<a href="sql.php?lang=en&server=1&db=webtrack&table=users&pos=0&session_max_rows=30&disp_direction=horizontal&repeat_cells=100&dontlimitchars=&sql_query=SELECT+%2A+FROM+%60users%60+ORDER+BY+%60pwd%60+ASC">
pwd</a>
</th>
<th>
<a href="sql.php?lang=en&server=1&db=webtrack&table=users&pos=0&session_max_rows=30&disp_direction=horizontal&repeat_cells=100&dontlimitchars=&sql_query=SELECT+%2A+FROM+%60users%60+ORDER+BY+%60name%60+ASC">
name</a>
</th>
</tr>
<!-- Results table body -->
Alternatively, you could just return the field name and sort order to your page which then appends them to your base query