MySQLi Tutorial MySQLi References

mysqli_result $field_count Property



The mysqli_result::$field_count / mysqli_num_fields() function is used to get the number of fields in the result set.

Syntax

//Object-oriented style
$mysqli_result->field_count;

//Procedural style
mysqli_num_fields(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 int representing the number of fields.

Example: Object-oriented style

The example below shows the usage of mysqli_result::$field_count property.

<?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 EmpID, Name, Age FROM Employee ORDER BY Age";
$result = $mysqli->query($sql);

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

//getting the number of fields in the result set
$field_cnt = $result->field_count;

printf("Result set has %d fields.\n", $field_cnt);
?>

The output of the above code will be similar to:

Result set has 3 fields.

Example: Procedural style

The example below shows the usage of mysqli_num_fields() 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 EmpID, Name, Age FROM Employee ORDER BY Age";
$result = mysqli_query($mysqli, $sql);

//closing the connection
mysqli_close($mysqli);

//getting the number of fields in the result set
$field_cnt = mysqli_num_fields($result);

printf("Result set has %d fields.\n", $field_cnt);
?>

The output of the above code will be similar to:

Result set has 3 fields.

❮ MySQLi Functions Reference