PHP mysqli_fetch_field() Function

PHP mysqli_fetch_field() function returns the next field in the result set, as an object.

Syntax

object mysqli_fetch_field(result);;

mysqli_fetch_field() Function Parameter

ParameterDescription
result :Required parameter. Mention a result set identifier returned by mysqli_query().

mysqli_fetch_field() Function Return Value

Return Values :Returns an object containing field information. The properties of the object are:

  • name - column name
  • table - name of the table the column belongs to, which is the alias name if one is defined
  • max_length - maximum length of the column
  • not_null - 1 if the column cannot be NULL
  • primary_key - 1 if the column is a primary key
  • unique_key - 1 if the column is a unique key
  • multiple_key - 1 if the column is a non-unique key
  • numeric - 1 if the column is numeric
  • blob - 1 if the column is a BLOB
  • type - the type of the column
  • unsigned - 1 if the column is unsigned
  • zerofill - 1 if the column is zero-filled

mysqli_fetch_field() Function Example

<?php
$con = mysqli_fetch_fieldt("localhost","user","password","db");

if (mysqli_connect_errno())
{
	echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql = "SELECT fname, lname FROM employee";
$sql_result = mysqli_query($con, $sql);

while ($field_info = mysqli_fetch_field($sql_result)) {
  echo "Name : " . $field_info->name;
  echo "Table Name: " . $field_info->table;
}
  
mysql_free_result($sql_result);

mysqli_close($con);
?>