PHP - Fatal error: Unsupported operand types
You are hitting a classic "undefined behavior" trap in PHP, one that often confuses developers because the code looks syntactically correct on the surface. The error message Fatal error: Unsupported operand types on line 103 points directly to this specific calculation:
$avg = (round($total_rating_points / $total_ratings,1));
At first glance, you are dividing a number by another number. However, the reality of how PHP handles MySQL queries and data fetching is where the logic breaks down. The error occurs because one of your variablesâlikely $total_ratings or $total_rating_pointsâis not an integer or float as expected, but rather a string containing non-numeric characters, or worse, NULL.
The Hidden Culprit: MySQL Data Types
The root cause lies in how you are fetching data from the database. Look closely at your first query execution block:
$result = mysqli_query($dbc,$sql1);
// ... error checking ...
$total_ratings = mysqli_fetch_array($result);
Here is the critical mistake: mysqli_fetch_array() returns an associative array or a numeric indexed array, not a scalar value. When you assign this entire array to $total_ratings, PHP treats it as an array object (or converts it to a string representation like "Array" when used in arithmetic operations).
When PHP attempts to execute $total_rating_points / $total_ratings, it is trying to divide a number by the string "Array". Since "Array" cannot be cast into a valid numeric type for division, PHP throws the Unsupported operand types fatal error.
How to Fix It
To resolve this, you must explicitly extract the scalar value from the result set before performing calculations. Your SQL query SELECT COUNT(users_articles_id) returns a single column. You need to access that specific index or column name.
If you are using the default numeric index (the first column returned), update your assignment like this:
$total_ratings = mysqli_fetch_array($result)[0];
// OR
$total_ratings = mysqli_fetch_assoc($result)['COUNT(users_articles_id)'];
However, there is a second potential issue lurking in your code logic. If the database returns NULL (which happens if no rows exist or if the count logic fails), $total_ratings becomes NULL. In PHP, dividing by NULL also results in an error because NULL cannot be treated as a number for division unless explicitly cast.
You can safeguard against this by using floatval() or ensuring the variable is cast to a numeric type:
$total_ratings = (float) mysqli_fetch_array($result)[0];
Improving Query Safety
While fixing the immediate error involves correcting the data extraction, relying on string interpolation in your SQL queries (like WHERE users_articles_id = '$page') is risky and prone to SQL injection. Since you are using prepared statements in modern Laravel projects, it is worth noting that this pattern is deprecated in favor of parameter binding.
If you were migrating this logic to a framework like Laravel, you would see the query handled differently:
$articles = DB::table('articles_grades')
->where('users_articles_id', $page) // Passed as a parameter, not concatenated
->count();
This ensures that $total_ratings is always an integer. If the count is zero (which COUNT() returns as 0 rather than NULL for empty sets), your logic will hold up better because dividing by zero in PHP results in Infinity, which might be acceptable or require specific handling depending on your business logic, but it won't trigger a type error unless mixed with strings.
Final Implementation Check
Review your second query block as well. The same issue applies:
$total_rating_points = array_sum($trp);
Ensure that $trp contains only numeric values derived from the grade_points column. If any row has a NULL value for grade_points, array_sum() will return NULL. When you then try to divide this NULL by your corrected integer $total_ratings, you will encounter the same fatal error again.
Always initialize your accumulators or check for NULLs immediately after fetching:
$total_rating_points = 0;
while($row = mysqli_fetch_array($result)) {
if ($row[0] !== null) {
$total_rating_points += (float)$row[0];
}
}
By ensuring both operands in your division are strictly numeric integers or floats, the Unsupported operand types error will disappear.
