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, 해당 행이 unsigned 숫자면
  • zerofill - 1, 해당 행이 zero-filled이면

문법

mysql_fetch_field(datafield_offset)
, 설명
data 필수. 사용할 데이터 포인터입니다. 이 데이터 포인터는 mysql_query()이 반환한 결과에서 옵니다.
field_offset 필수. 시작할 필드를 지정합니다. 0은 첫 번째 필드를 의미합니다. 설정되지 않으면 다음 필드를 가져옵니다.

피드백과 주석

주석:이 함수가 반환하는 필드 이름은 대소문자를 구분합니다.

예제

<?php
$con = mysql_connect("localhost", "hello", "321");
if (!$con)
  {
  die('연결할 수 없음: ' . 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 "필드 이름: " . $property->name . "<br />";
  echo "테이블 이름: " . $property->table . "<br />";
  echo "기본 값: " . $property->def . "<br />";
  echo "최대 길이: " . $property->max_length . "<br />";
  echo "NULL이 아님: " . $property->not_null . "<br />"; 
  echo "주 키: " . $property->primary_key . "<br />";
  echo "유일 키: " . $property->unique_key . "<br />"; 
  echo "다중 키: " . $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