PHP Function Reference

PHP mysqli_stmt $field_count Property



The PHP mysqli_stmt::$field_count / mysqli_stmt_field_count() function returns the number of columns in the prepared statement.

Syntax

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

//Procedural style
mysqli_stmt_field_count(statement)

Parameters

statement Required. For procedural style only: Specify a mysqli_stmt object returned by mysqli_stmt_init().

Return Value

Returns an integer representing the number of columns.

Example: Object-oriented style

The example below shows the usage of mysqli_stmt::$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();
}

//preparing an SQL statement for execution
$query = "SELECT EmpID, Name, Age FROM Employee ORDER BY Age";
$stmt = $mysqli->prepare($query);

//executing the SQL statement
$stmt->execute();

//storing the result in an internal buffer
$stmt->store_result();

//getting the number of fields in the prepared statement
$field_cnt = $stmt->field_count;

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

The output of the above code will be:

Statement has 3 fields.

Example: Procedural style

The example below shows the usage of mysqli_stmt_field_count() 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();
}

//preparing an SQL statement for execution
$query = "SELECT EmpID, Name, City FROM Employee ORDER BY Age";
$stmt = mysqli_prepare($mysqli, $query);

//executing the SQL statement
mysqli_stmt_execute($stmt);

//storing the result in an internal buffer
mysqli_stmt_store_result($stmt);

//getting the number of fields in the prepared statement
$field_cnt = mysqli_stmt_field_count($stmt);

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

The output of the above code will be:

Statement has 3 fields.

❮ PHP MySQLi Reference