PHP mysqli_field_count() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_field_count() returns how many columns the most recent query on a connection produced. It is the common way to learn the field count after a SELECT.

On a mysqli_result you can also read the field_count property. Procedurally, pass the connection after mysqli_query() (or similar). The object styles are $mysqli->field_count and $result->field_count.

Syntax

Call mysqli_field_count() with the values you need. Start with this line.

PHP
$n = mysqli_field_count($mysqli);
CallWhat it does
mysqli_field_count($mysqli)Returns the column count for the latest query on that connection.
$result->field_countReads the column count from a mysqli_result object.
$mysqli->field_countOO form on the connection after a query.

After a SELECT, the number matches how many columns that result has. For non-SELECT statements the count is often 0. Teach the post-query connection/result count—that is the everyday use.

Library declarations
PHP
mysqli_field_count(mysqli $mysql): int

Parameters

ParameterDescription
mysqlRequired. The mysqli connection that just ran a query.

Return Value

Return ValueDescription
intThe number of columns for the most recent query on that connection.

PHP Examples

Example 1 — Count Columns After a SELECT

Run a three-column query and print mysqli_field_count() from a fixed demo.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
mysqli_query($mysqli, "SELECT id, name, price FROM products LIMIT 1");

echo mysqli_field_count($mysqli), PHP_EOL;

mysqli_close($mysqli);

How It Works

  1. The SELECT lists three columns.
  2. mysqli_field_count() reads that count from the connection.
  3. The sample prints 3.
  4. You can also check $result->field_count if you kept the result handle.

Example 2 — Use the Result Property

Show the same width via $result->field_count after storing the result.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT id, email FROM users LIMIT 1");

echo $result->field_count, PHP_EOL;

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

How It Works

  1. mysqli_query() returns a mysqli_result for the SELECT.
  2. The field_count property on the result is 2.
  3. This matches mysqli_field_count($mysqli) for that query.
  4. Free the result when you are finished.

Example 3 — Non-SELECT Queries Often Report Zero

After an UPDATE, field_count is typically 0 because no result columns were produced.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
mysqli_query($mysqli, "UPDATE products SET price = price WHERE id = 1");

echo mysqli_field_count($mysqli), PHP_EOL;

mysqli_close($mysqli);

How It Works

  1. UPDATE does not return a result set of columns.
  2. mysqli_field_count() therefore reports 0 in this sample.
  3. Use mysqli_affected_rows() to learn how many rows changed.
  4. Reserve field_count for SELECT-style results.

Frequently Asked Questions

It returns the number of columns produced by the most recent query on a mysqli connection.
Both exist in OO form. The procedural mysqli_field_count() takes the connection. Results also expose a field_count property.
Read $mysqli->field_count after a query, or $result->field_count on a mysqli_result.
Columns (fields). Row count is mysqli_num_rows() on a stored result.
Those statements do not return a SELECT-style field list, so the field count is commonly 0.

Next: mysqli_field_seek()

mysqli_field_seek() moves the field cursor inside a mysqli_result to a given zero-based co.

mysqli_field_seek() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful