Decoding "Cannot use object of type stdClass as array"
This fatal error usually signals a mismatch between how you are expecting PHP to return data from a database query and how it actually behaves. In your specific case, the crash happens on line 183 because $getvidids->result() returns an object, not an array. When you attempt to access $row['videoid'], PHP tries to treat that object like an associative array, which triggers the exception.
To understand why this breaks, we need to look at how CakePHP's Query Builder handles result sets by default. The method ->result() is designed to return results as a collection of stdClass objects. Each row in your query becomes a distinct object where properties are accessed via magic methods or camelCase attributes, not square bracket notation.
When you write $row['videoid'], PHP looks for an array key named 'videoid'. Since $row is actually an object, it searches for a property named videoid instead. However, the error message explicitly states "Cannot use object of type stdClass as array," which implies that somewhere in your logic or environment, the code is forcing an array access pattern on an object without conversion.
In many modern frameworks, including Laravel and newer versions of CakePHP, the preferred method for fetching results is ->toArray() or simply relying on the default behavior if you aren't explicitly calling a method that returns objects. If you switch to $getvidids->getResultArray(), the return type changes from stdClass objects to an associative array. This allows direct access via $row['videoid'] without triggering the fatal error.
Switching to Array Accessors
The most straightforward fix is changing your query call:
$result = $ci->db->query(
"SELECT * FROM videogroupids
WHERE videogroupid='$videogroup' AND used='0' LIMIT 10"
);
// Get results as an array of arrays
$rows = $result->getResultArray();
foreach ($rows as $row) {
// Now this works because $row is an array, not an object
$vidid = $row['videoid'];
}
Alternatively, if you are using the query() method which might return a Query Builder instance depending on your CakePHP version and configuration, ensure you are executing it correctly. Sometimes developers confuse query() (which returns a result object) with methods that return raw rows. If you are working in an environment where ->result() is mandatory but you need array access, you must convert the collection first:
$objects = $getvidids->result();
$rowsArray = json_decode(json_encode($objects), true); // Converts objects to arrays
foreach ($rowsArray as $row) {
$vidid = $row['videoid'];
}
However, the getResultArray() approach is cleaner and less prone to serialization errors. It aligns your data structure expectations with standard PHP array syntax.
Security Considerations in Your Query
While fixing the object/array mismatch resolves the crash, there is a critical security issue in your SQL string. You are currently concatenating user input directly into the query:
"WHERE videogroupid='$videogroup' AND used='0'"
This practice opens your application to SQL injection attacks. Even if the immediate error isn't about security, fixing the data type issue should be paired with parameter binding. In CakePHP, the Query object supports prepared statements natively. You should rewrite the query using parameters:
$query = $ci->db->query("SELECT * FROM videogroupids WHERE videogroupid = ? AND used = ?", [$videogroup, '0']);
This ensures that values are treated strictly as data, not executable SQL code. If you are transitioning from legacy CakePHP to Laravel (or vice versa), note that the syntax for fetching results differs significantly. In Laravel, DB::select() returns an array of associative arrays by default, so $row['videoid'] works out of the box without needing getResultArray().
When to Use Objects vs Arrays
It is worth noting that using stdClass objects isn't inherently bad; in fact, they offer a slightly more robust interface for complex data structures where you might use camelCase properties like $row->videoid. The issue here is strictly syntactic: you are trying to access array keys on an object. If your downstream code relies heavily on associative array syntax (like passing it to a function expecting array), converting via getResultArray() is the standard pattern.
If you encounter this error again, check the return type of your query method first. If it returns objects but you need arrays, convert them immediately after fetching. If you are maintaining older codebases, remember that newer frameworks often deprecate object-based result sets in favor of arrays to simplify array manipulation functions.