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. $db_selected = mysql_select_db("test_db",$con);
  8. $sql = "SELECT * from Person";
  9. $result = mysql_query($sql,$con);
  10. while ($property = mysql_fetch_field($result))
  11. {
  12. echo "Field name: " . $property->name . "<br />";
  13. echo "Table name: " . $property->table . "<br />";
  14. echo "Default value: " . $property->def . "<br />";
  15. echo "Max length: " . $property->max_length . "<br />";
  16. echo "Not NULL: " . $property->not_null . "<br />";
  17. echo "Primary Key: " . $property->primary_key . "<br />";
  18. echo "Unique Key: " . $property->unique_key . "<br />";
  19. echo "Mutliple Key: " . $property->multiple_key . "<br />";
  20. echo "Numeric Field: " . $property->numeric . "<br />";
  21. echo "BLOB: " . $property->blob . "<br />";
  22. echo "Field Type: " . $property->type . "<br />";
  23. echo "Unsigned: " . $property->unsigned . "<br />";
  24. echo "Zero-filled: " . $property->zerofill . "<br /><br />";
  25. }
  26. mysql_close($con);
  27. ?>

输出:

  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. Field name: FirstName
  15. Table name: Person
  16. Default value:
  17. Max length: 7
  18. Not NULL: 0
  19. Primary Key: 0
  20. Unique Key: 0
  21. Mutliple Key: 0
  22. Numeric Field: 0
  23. BLOB: 0
  24. Field Type: string
  25. Unsigned: 0
  26. Zero-filled: 0
  27. Field name: City
  28. Table name: Person
  29. Default value:
  30. Max length: 9
  31. Not NULL: 0
  32. Primary Key: 0
  33. Unique Key: 0
  34. Mutliple Key: 0
  35. Numeric Field: 0
  36. BLOB: 0
  37. Field Type: string
  38. Unsigned: 0
  39. Zero-filled: 0
  40. Field name: Age
  41. Table name: Person
  42. Default value:
  43. Max length: 2
  44. Not NULL: 0
  45. Primary Key: 0
  46. Unique Key: 0
  47. Mutliple Key: 0
  48. Numeric Field: 1
  49. BLOB: 0
  50. Field Type: int
  51. Unsigned: 0
  52. Zero-filled: 0

分类导航