PHP mysql_fetch_field() Function
Definition and Usage
The mysql_fetch_field() function retrieves column information from the result set and returns it as an object.
mysql_fetch_field() can be used to obtain field information from the query result. If no field offset is specified, the next field that has not been obtained by mysql_fetch_field() is extracted.
This function returns an object containing field information.
The properties of the returned object are:
- name - the column name
- table - the name of the table where the column is located
- max_length - the maximum length of the column
- not_null - 1, if the column cannot be NULL
- primary_key - 1, if the column is a primary key
- unique_key - 1, if the column is a unique key
- multiple_key - 1, if the column is a non-unique key
- numeric - 1, if the column is numeric
- blob - 1,如果该列是 BLOB
- blob - 1, if the column is BLOB
- type - the type of the column
- unsigned - 1, if the column is unsigned
zerofill - 1, if the column is zero-filled
Syntaxdatamysql_fetch_field(field_offset)
, | Description |
---|---|
data | Required. The data pointer to use. This data pointer is from the mysql_query() return result. |
field_offset | Required. Specifies the field from which to start. 0 indicates the first field. If not set, the next field is retrieved. |
Tips and Comments
Note:The field name returned by this function is case-sensitive.
Example
<?php $con = mysql_connect("localhost", "hello", "321"); if (!$con) { die('Could not connect: ' . mysql_error()); } $db_selected = mysql_select_db("test_db",$con); $sql = "SELECT * from Person"; $result = mysql_query($sql,$con); while ($property = mysql_fetch_field($result)) { echo "Field name: " . $property->name . "<br />"; echo "Table name: " . $property->table . "<br />"; echo "Default value: " . $property->def . "<br />"; echo "Max length: " . $property->max_length . "<br />"; echo "Not NULL: " . $property->not_null . "<br />"; echo "Primary Key: " . $property->primary_key . "<br />"; echo "Unique Key: " . $property->unique_key . "<br />"; echo "Mutliple Key: " . $property->multiple_key . "<br />"; echo "Numeric Field: " . $property->numeric . "<br />"; echo "BLOB: " . $property->blob . "<br />"; echo "Field Type: " . $property->type . "<br />"; echo "Unsigned: " . $property->unsigned . "<br />"; echo "Zero-filled: " . $property->zerofill . "<br /><br />"; } mysql_close($con); ?>
Output:
Field name: LastName Table name: Person Default value: Max length: 8 Not NULL: 0 Primary Key: 0 Unique Key: 0 Mutliple Key: 0 Numeric Field: 0 BLOB: 0 Field Type: string Unsigned: 0 Zero-filled: 0 Field name: FirstName Table name: Person Default value: Max length: 7 Not NULL: 0 Primary Key: 0 Unique Key: 0 Mutliple Key: 0 Numeric Field: 0 BLOB: 0 Field Type: string Unsigned: 0 Zero-filled: 0 Field name: City Table name: Person Default value: Max length: 9 Not NULL: 0 Primary Key: 0 Unique Key: 0 Mutliple Key: 0 Numeric Field: 0 BLOB: 0 Field Type: string Unsigned: 0 Zero-filled: 0 Field name: Age Table name: Person Default value: Max length: 2 Not NULL: 0 Primary Key: 0 Unique Key: 0 Mutliple Key: 0 Numeric Field: 1 BLOB: 0 Field Type: int Unsigned: 0 Zero-filled: 0