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

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

[code lang="php"]
function getFieldNames($database,$connect_string,$table){

$query_getfields = "SHOW COLUMNS FROM ".$table;
$getfields = mysql_query($query_getfields, $connect_string) or die(mysql_error());
$row_getfields = mysql_fetch_assoc($getfields);
$all_getfields = mysql_query($query_getfields);

$fields = array();
do {
array_push($fields,$row_getfields['Field']);
} while($row_getfields = mysql_fetch_assoc($getfields));

return $fields;

}[/code]

Tags:
Post comment as twitter logo facebook logo
Sort: Newest | Oldest