PHP Function Reference

PHP mysqli_result fetch_field() Method



The PHP mysqli_result::fetch_field() / mysqli_fetch_field() function returns the definition of one column of a result set as an object. This function can be called repeatedly to retrieve information about all columns in the result set.

Syntax

//Object-oriented style
public mysqli_result::fetch_field()

//Procedural style
mysqli_fetch_field(result)

Parameters

result Required. For procedural style only: Specify a mysqli_result object returned by mysqli_query(), mysqli_store_result(), mysqli_use_result() or mysqli_stmt_get_result().

Return Value

Returns an object which contains field definition information or false if no field information is available.

Object properties

PropertyDescription
nameThe name of the column
orgnameOriginal column name if an alias was specified
tableThe name of the table this field belongs to (if not calculated)
orgtableOriginal table name if an alias was specified
defReserved for default value, currently always ""
dbThe name of the database
catalogThe catalog name, always "def"
max_lengthThe maximum width of the field for the result set
lengthThe width of the field, as specified in the table definition
charsetnrThe character set number for the field
flagsAn integer representing the bit-flags for the field
typeThe data type used for this field
decimalsThe number of decimals used (for integer fields)

Example: Object-oriented style

The example below shows the usage of mysqli_result::fetch_field() method.

<?php
//establishing connection to the database
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
  echo "Failed to connect to MySQL: ". $mysqli->connect_error;
  exit();
}

//getting query result from the database
$sql = "SELECT Name, Age FROM Employee ORDER BY Age";

if ($result = $mysqli->query($sql)) {

  //getting field information for all columns
  while ($finfo = $result->fetch_field()) {

    //getting fieldpointer offset
    $currentfield = $result->current_field;

    printf("Column %d:\n", $currentfield);
    printf("Name:     %s\n", $finfo->name);
    printf("Table:    %s\n", $finfo->table);
    printf("max. Len: %d\n", $finfo->max_length);
    printf("Flags:    %d\n", $finfo->flags);
    printf("Type:     %d\n\n", $finfo->type);
  }

  //free the memory associated with the result
  $result->close();
}

//closing the connection
$mysqli->close();
?>

The output of the above code will be similar to:

Column 1:
Name:     Name
Table:    Employee
max. Len: 50
Flags:    1
Type:     254

Column 2:
Name:     Age
Table:    Employee
max. Len: 10
Flags:    32769
Type:     4

Example: Procedural style

The example below shows the usage of mysqli_fetch_field() function.

<?php
//establishing connection to the database
$mysqli = mysqli_connect("localhost", "user", "password", "database");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: ". mysqli_connect_error();
  exit();
}

//getting query result from the database
$sql = "SELECT Name, Age FROM Employee ORDER BY Age";

if ($result = mysqli_query($mysqli, $sql)) {

  //getting field information for all columns
  while ($finfo = mysqli_fetch_field($result)) {

    //getting fieldpointer offset
    $currentfield = mysqli_field_tell($result);

    printf("Column %d:\n", $currentfield);
    printf("Name:     %s\n", $finfo->name);
    printf("Table:    %s\n", $finfo->table);
    printf("max. Len: %d\n", $finfo->max_length);
    printf("Flags:    %d\n", $finfo->flags);
    printf("Type:     %d\n\n", $finfo->type);
  }

  //free the memory associated with the result
  mysqli_free_result($result);
}

//closing the connection
mysqli_close($mysqli);
?>

The output of the above code will be similar to:

Column 1:
Name:     Name
Table:    Employee
max. Len: 50
Flags:    1
Type:     254

Column 2:
Name:     Age
Table:    Employee
max. Len: 10
Flags:    32769
Type:     4

❮ PHP MySQLi Reference