Current Location: Home> Latest Articles> mysqli_result::$field_count Possible cause of undefined or error

mysqli_result::$field_count Possible cause of undefined or error

gitbox 2025-05-29

When using PHP for database development, mysqli extension is one of the most commonly used database operations. The mysqli_result::$field_count property is used to get the number of fields (columns) in the result set. However, in actual development, developers may encounter an error or undefined situation where mysqli_result::$field_count is reported. This article will analyze the common causes of this problem and provide corresponding solutions.

1. Common error messages

Developers may encounter error prompts similar to the following:

 Fatal error: Uncaught Error: Undefined property: mysqli_result::$field_count in ...

or:

 Trying to access array offset on value of type bool

These error prompts indicate that when trying to access the field_count property, the mysqli_result object is not returned correctly, and may not even be an object.

2. Analysis of possible causes

1. Return false instead of mysqli_result object if query fails

When SQL query execution fails, the mysqli::query() method returns false instead of a mysqli_result object. If the next code defaults to it as a result set object, an error will naturally occur when accessing the attribute.

Sample code:

 $mysqli = new mysqli('localhost', 'root', '', 'test');
$result = $mysqli->query("SELECT * FROM non_existing_table");

echo $result->field_count; // Report an error:Not an object

Solution:

Before accessing the field_count property, first determine whether the query is successful:

 if ($result instanceof mysqli_result) {
    echo $result->field_count;
} else {
    echo "Query failed:" . $mysqli->error;
}

2. The query statement does not return the result set (such as INSERT, UPDATE)

Some SQL statements such as INSERT , UPDATE , and DELETE will not return the result set. At this time, query() returns true , and accessing field_count will also cause an error.

Sample code:

 $result = $mysqli->query("UPDATE users SET name = 'test' WHERE id = 1");
echo $result->field_count; // Report an error:Not an object

Solution:

Distinguish different types of SQL statements and use field_count only for queries that return mysqli_result objects:

 $result = $mysqli->query("SELECT * FROM users");
if ($result instanceof mysqli_result) {
    echo "Number of columns:" . $result->field_count;
} else {
    echo "Not a query result set,Execution status:" . ($result ? 'success' : 'fail');
}

3. The PHP or mysqli extension version is too old or not enabled

Some older versions of PHP or the mysqli extension is not enabled correctly may cause the relevant properties to be unavailable.

Solution:

Confirm the PHP and mysqli extension versions:

 phpinfo();

Make sure mysqli extension is enabled, and it is recommended that PHP version be 7.4 and above.

4. Error using static access

field_count is an instance property, and incorrectly accessing it using static mode will result in undefined or errors.

Error example:

 echo mysqli_result::$field_count; // mistake

The correct way:

 echo $result->field_count;

5. Error parsing JSON, API response or other object disguise

In some cases, developers may mistakenly parse external data as mysqli_result , such as data fetched from the API:

 $response = file_get_contents("https://gitbox.net/api/data");
$data = json_decode($response);

echo $data->field_count; // Actually not mysqli_result Object

Solution:

Make sure you are using the correct type object and make type judgments:

 if ($data instanceof mysqli_result) {
    echo $data->field_count;
} else {
    echo "类型mistake,Unable to access field_count";
}

3. Recommended general judgment structure

In order to enhance the robustness of the code, it is recommended to encapsulate the query logic uniformly:

 function safe_query($mysqli, $sql) {
    $result = $mysqli->query($sql);
    if ($result instanceof mysqli_result) {
        return $result;
    } else {
        error_log("SQL Error: " . $mysqli->error);
        return false;
    }
}

// How to use
$result = safe_query($mysqli, "SELECT * FROM users");
if ($result) {
    echo "Number of columns:" . $result->field_count;
}

4. Summary

The reason why mysqli_result::$field_count reports an error or is undefined is often because the developer does not correctly handle the return result type of query() , or the usage environment and calling method are unclear. Through the above methods, developers can effectively avoid such errors and write safer and more robust PHP database operation code.