Name

mysql_fetch_field( )

Synopsis

mysql_fetch_field(results[, offset])

This returns an object containing information about a field from a results set given. Information is given on the first field of a results set waiting to be returned; the function can be called repeatedly to report on each field of a SELECT statement. A number may be given as the second argument to skip one or more fields. The elements of the object are as follows: name for column name; table for table name, max_length for the maximum length of the column; not_null, which has a value of 1 if the column may not have a NULL value; primary_key, which has a value of 1 if the column is a primary key column; unique_key, which returns 1 if it’s a unique key; multiple_key, which returns 1 if it’s not unique; numeric, which returns 1 if it’s a numeric datatype; blob, which returns 1 if it’s a BLOB datatype; type, which returns the datatype; unsigned, which returns 1 if the column is unsigned; and zerofill, which returns 1 if it’s a zero-fill column.

...
$sql_stmnt = "SELECT * FROM workreq LIMIT 1";
$results = mysql_db_query('workrequests', $sql_stmnt);
$num_fields = mysql_num_fields($results);
for ($index = 0; $index < $num_fields; $index++) {
  $info = mysql_fetch_field($results, $index);
  print "$info->name  ($info->type $info->max_length) \n";
}
...

Here all of the columns for one record are selected and placed in $results. The number of fields is determined by mysql_num_fields( ) for the for statement that follows. The

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.