Home > Article > Backend Development > How to Fix the \"Cannot Use Object of Type mysqli_result as Array\" Error in PHP?
To resolve the error message "Fatal error: Cannot use object of type mysqli_result as array," which typically occurs in PHP code related to database operations, follow these steps:
Understanding the Error:
The error indicates that you are attempting to use a mysqli_result object as an array when it should be treated as an object returned by a database query.
Inspecting the Code:
In your case, the code in line 303 attempts to use $followingdata['usergroupid'] as an array element, but since it is a mysqli_result object, it cannot be used directly as an array.
Solution:
To fix the issue, you need to fetch the result row from the $result object as an associative array. This can be achieved using either mysqli_fetch_assoc() or mysqli_fetch_array().
Corrected Code:
Instead of accessing the usergroupid property directly, use mysqli_fetch_assoc() to fetch the result row as an associative array:
<code class="php">$result = $vbulletin->db->query_write("SELECT * FROM user WHERE userid = $followinginfo[userid]"); $followingdata = $result->fetch_assoc();</code>
Alternatively:
You can also use mysqli_fetch_array() to fetch the result row as an array:
<code class="php">$followingdata = $result->fetch_array(MYSQLI_ASSOC);</code>
Applying the Solution:
Make the necessary changes in your code, and the error should be resolved. Remember to fetch the result row as an associative array before accessing it as an array.
The above is the detailed content of How to Fix the \"Cannot Use Object of Type mysqli_result as Array\" Error in PHP?. For more information, please follow other related articles on the PHP Chinese website!