Name

mysql_list_fields( )

Synopsis

mysql_list_fields(database, table[, connection])

This returns a results set containing information about the columns of a table given for a database specified. The mysql_field_flags( ), mysql_field_len(), mysql_field_name( ), and mysql_field_type( ) functions can be used to extract information from the results set. An identifier may be given as a third argument to the function to retrieve information for a different MySQL connection.

...
$fields = mysql_list_fields('workrequests', 'workreq');
$num_fields = mysql_num_fields($fields);
for ($index = 0; $index < $num_fields; $index++) {
  print mysql_field_name($fields, $index) . "\n";
}
...

After connecting to MySQL, in the first line the example uses mysql_list_fields( ) to retrieve a list of column names from the database and table given as arguments. To assist the for statement that follows, the mysql_num_fields( ) function determines the number of fields in the results set, returning a field for each column. Then PHP loops through the for statement for all the fields and displays the name of each column using mysql_field_name( ). Here are a few lines from the output of this script:

wrid
wr_date
clientid
...

Get MySQL in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.