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.
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.
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;
}
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');
}
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.
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;
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";
}
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;
}
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.