W3docs

field_count

In this article, we will focus on the mysqli_field_count() function in PHP, which is used to return the number of columns for the most recent query executed by

The mysqli_field_count() function returns the number of columns produced by the most recent query run on a MySQLi connection. It is a quick way to inspect a result set's shape without holding on to the result object — a handy check after running a SELECT, or after a query whose column count you want to validate before fetching rows.

This article covers the function's syntax, its parameters and return value, how it differs from the closely related mysqli_num_fields(), and runnable examples (both procedural and object-oriented).

Syntax

// Procedural style
mysqli_field_count(mysqli $mysql): int

// Object-oriented style
$mysqli->field_count: int   // property, read as $mysqli->field_count
PartDescription
$mysqlA connection link returned by mysqli_connect() (procedural style only).
Return valueAn int — the number of columns for the last query on that connection. Returns 0 when the last statement returned no result set (for example INSERT, UPDATE, or DELETE).

Note that the object-oriented form is a read-only property ($mysqli->field_count), not a method call.

How to use mysqli_field_count()

You call the function on a live connection right after running a query. There is no need to capture the result object — mysqli_field_count() reads the column count straight from the connection's last-query state.

<?php
$mysqli = mysqli_connect("localhost", "username", "password", "database");

if (mysqli_connect_errno()) {
    die("Connection failed: " . mysqli_connect_error());
}

$query = "SELECT id, name, email FROM users";
mysqli_query($mysqli, $query);

$num_fields = mysqli_field_count($mysqli);
printf("Number of columns: %d\n", $num_fields);  // Number of columns: 3

mysqli_close($mysqli);
?>

Here we connect with mysqli_connect() and check mysqli_connect_errno() to bail out cleanly on failure. We run a SELECT that returns three columns, then read the column count from the connection. The output is Number of columns: 3.

mysqli_field_count() vs mysqli_num_fields()

These two functions answer the same question — "how many columns?" — but read it from different places:

  • mysqli_field_count($mysqli) takes the connection and reports the columns of the most recent query. Use it when you do not have (or do not want) the result object.
  • mysqli_num_fields($result) takes a result object returned by mysqli_query(). Use it when you already hold the result and are about to iterate it.
<?php
$mysqli = mysqli_connect("localhost", "user", "pass", "shop");

$result = mysqli_query($mysqli, "SELECT id, title, price FROM products");

// From the result object:
echo mysqli_num_fields($result), "\n";   // 3

// From the connection (same last query):
echo mysqli_field_count($mysqli), "\n";  // 3

mysqli_free_result($result);
mysqli_close($mysqli);
?>

Common use case: validate column count

A typical use is guarding code that expects a specific result shape — for example, confirming a query returned the columns you depend on before you start reading them:

<?php
$mysqli = mysqli_connect("localhost", "user", "pass", "shop");

mysqli_query($mysqli, "SELECT id, name FROM categories");

if (mysqli_field_count($mysqli) === 2) {
    echo "Result has the expected 2 columns.\n";
} else {
    echo "Unexpected column count.\n";
}

mysqli_close($mysqli);
?>

Things to watch for

  • It reports columns, not rows. For the number of rows changed by a write, use mysqli_affected_rows(); for an auto-increment key after an insert, use mysqli_insert_id().
  • It reflects only the last query on the connection. If you run another query in between, the count updates.
  • A non-result statement (INSERT/UPDATE/DELETE) leaves the count at 0.

Conclusion

mysqli_field_count() is a lightweight way to read the number of columns from the most recent query on a MySQLi connection — without keeping the result object around. Reach for it to validate a result's shape, and prefer mysqli_num_fields() when you already hold the result object. To explore the related result-handling functions, see mysqli_query() and mysqli_fetch_field().

Practice

Practice
What is the function of 'field_count' in PHP?
What is the function of 'field_count' in PHP?
Was this page helpful?