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 name of the column
  • 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 primary key
  • unique_key - 1, if the column is unique key
  • multiple_key - 1, if the column is non-unique key
  • numeric - 1, if the column is numeric
  • blob - 1,如果该列是 BLOB
  • blob - 1، إذا كان العمود هو BLOB
  • type - نوع العمود
  • unsigned - 1، إذا كان العمود غير مرتبط

zerofill - 1، إذا كان العمود محملًا بالصفر

القواعدdatamysql_fetch_field(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 . "
"; echo "BLOB: " . $property->blob . "
"; echo "Field Type: " . $property->type . "
"; echo "Unsigned: " . $property->unsigned . "
"; echo "Zero-filled: " . $property->zerofill . "

"; } 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