PHP mysql_fetch_field() 函數
定義和用法
mysql_fetch_field() 函數從結果集中取得列信息并作為對象返回。
mysql_fetch_field() 可以用來從查詢結果中取得字段的信息。如果沒有指定字段偏移量,則提取下一個尚未被 mysql_fetch_field() 取得的字段。
該函數返回一個包含字段信息的對象。
被返回的對象的屬性為:
- name - 列名
- table - 該列所在的表名
- max_length - 該列最大長度
- not_null - 1,如果該列不能為 NULL
- primary_key - 1,如果該列是 primary key
- unique_key - 1,如果該列是 unique key
- multiple_key - 1,如果該列是 non-unique key
- numeric - 1,如果該列是 numeric
- blob - 1,如果該列是 BLOB
- type - 該列的類型
- unsigned - 1,如果該列是無符號數
- zerofill - 1,如果該列是 zero-filled
語法
mysql_fetch_field(data,field_offset)
參數 | 描述 |
---|---|
data | 必需。要使用的數據指針。該數據指針是從 mysql_query() 返回的結果。 |
field_offset | 必需。規定從哪個字段開始。0 指示第一個字段。如果未設置,則取回下一個字段。 |
提示和注釋
注釋:本函數返回的字段名是區分大小寫的。
實例
<?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); ?>
輸出:
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