Name

mysql_field_type( )

Synopsis

mysql_field_type(results, index)

This returns the column datatype for a field from the results set given. To specify a particular field, an offset is given as the second argument.

...
$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++) {
  $name = mysql_field_name($results, $index);
  $type = mysql_field_type($results, $index);
  print "$name - $type \n";
}
...

In this example, after one row of data is selected as a sample, the number of rows in the results set is determined using mysql_num_fields( ) so that a counter limit may be set up ($num_fields) in the for statement that follows. Within the for statement, the name of the field is extracted using mysql_field_name( ) and the datatype using mysql_field_type(). Here are a few lines of the output of this script:

wrid - int
wr_date - date
clientid - string
...

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.