How to get a list of a tables field / column names from MySQL using PHP – getFieldNames() Function

May 13th, 2008 by Oliver - Tagged with , - Posted in Web Development

(No Ratings Yet)
Loading ... Loading ...

A simple function to replicate the funcationality of the depreciated mysql_list_fields() function. It returns a simple array with a numeric index of the column / field names.

$database – String – The database you want to use
$connect_string – String – Your connection string
$table – String – The name of the table you wish to list the fields from

  1. function getFieldNames($database,$connect_string,$table){
  2.  
  3. $query_getfields = “SHOW COLUMNS FROM “.$table;
  4. $getfields = mysql_query($query_getfields, $connect_string) or die(mysql_error());
  5. $row_getfields = mysql_fetch_assoc($getfields);
  6. $all_getfields = mysql_query($query_getfields);
  7.  
  8. $fields = array();
  9. do {
  10. array_push($fields,$row_getfields[‘Field’]);
  11. } while($row_getfields = mysql_fetch_assoc($getfields));
  12.  
  13. return $fields;
  14.  
  15. }

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>