This is a function that does it - you'll have to pick the bones out of it yourself. Actually it's a method of a class, which if you've not done yet should still be able to work out.
And click the RTFM link in my sig to get the manual, it's all in the mysql functions
/**
* Fetches column names for a table or query
* @param string optional, otherwise uses last query result
* @return array
*/
function field_names($tableName = '')
{
$oldRes = $this->res;
if ($tableName)
{
$this->safe_query("SELECT * FROM $tableName LIMIT 0",
"MySQL->field_names ERROR: Table: $tableName does not exist<br>");
}
$numFields = mysql_num_fields($this->res);
for($i = 0; $i < $numFields; $i++)
$names[] = mysql_field_name($this->res, $i);
$this->res = $oldRes;
return $names;
}