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

语法

  1. mysql_fetch_field(data,field_offset)
参数 描述
data 必需。要使用的数据指针。该数据指针是从 mysql_query() 返回的结果。
field_offset 必需。规定从哪个字段开始。0 指示第一个字段。如果未设置,则取回下一个字段。

提示和注释

注释:本函数返回的字段名是区分大小写的。

例子

  1. <?php
  2. $con = mysql_connect("localhost", "hello", "321");
  3. if (!$con)
  4. {
  5. die('Could not connect: ' . mysql_error());
  6. }
  7.  
  8. $db_selected = mysql_select_db("test_db",$con);
  9.  
  10. $sql = "SELECT * from Person";
  11. $result = mysql_query($sql,$con);
  12.  
  13. while ($property = mysql_fetch_field($result))
  14. {
  15. echo "Field name: " . $property->name . "<br />";
  16. echo "Table name: " . $property->table . "<br />";
  17. echo "Default value: " . $property->def . "<br />";
  18. echo "Max length: " . $property->max_length . "<br />";
  19. echo "Not NULL: " . $property->not_null . "<br />";
  20. echo "Primary Key: " . $property->primary_key . "<br />";
  21. echo "Unique Key: " . $property->unique_key . "<br />";
  22. echo "Mutliple Key: " . $property->multiple_key . "<br />";
  23. echo "Numeric Field: " . $property->numeric . "<br />";
  24. echo "BLOB: " . $property->blob . "<br />";
  25. echo "Field Type: " . $property->type . "<br />";
  26. echo "Unsigned: " . $property->unsigned . "<br />";
  27. echo "Zero-filled: " . $property->zerofill . "<br /><br />";
  28. }
  29.  
  30. mysql_close($con);
  31. ?>

输出:

  1. Field name: LastName
  2. Table name: Person
  3. Default value:
  4. Max length: 8
  5. Not NULL: 0
  6. Primary Key: 0
  7. Unique Key: 0
  8. Mutliple Key: 0
  9. Numeric Field: 0
  10. BLOB: 0
  11. Field Type: string
  12. Unsigned: 0
  13. Zero-filled: 0
  14.  
  15. Field name: FirstName
  16. Table name: Person
  17. Default value:
  18. Max length: 7
  19. Not NULL: 0
  20. Primary Key: 0
  21. Unique Key: 0
  22. Mutliple Key: 0
  23. Numeric Field: 0
  24. BLOB: 0
  25. Field Type: string
  26. Unsigned: 0
  27. Zero-filled: 0
  28.  
  29. Field name: City
  30. Table name: Person
  31. Default value:
  32. Max length: 9
  33. Not NULL: 0
  34. Primary Key: 0
  35. Unique Key: 0
  36. Mutliple Key: 0
  37. Numeric Field: 0
  38. BLOB: 0
  39. Field Type: string
  40. Unsigned: 0
  41. Zero-filled: 0
  42.  
  43. Field name: Age
  44. Table name: Person
  45. Default value:
  46. Max length: 2
  47. Not NULL: 0
  48. Primary Key: 0
  49. Unique Key: 0
  50. Mutliple Key: 0
  51. Numeric Field: 1
  52. BLOB: 0
  53. Field Type: int
  54. Unsigned: 0
  55. Zero-filled: 0