CodeIgniter : "Fatal error: Cannot use object of type stdClass as array" message
There are times when the error message returned by the interpreter or compiler just don't make sense. Today I encountered an error message while I was coding up CodeIgniter today and the error message is :
Fatal error: Cannot use object of type stdClass as array
After reading up CodeIgniter user guide, apparently the error was caused by the wrong way of accessing result
array returned by the $this->db->query()
function.
To get rid of the error message, change the $row['username']
to $row->username
foreach ($results->result() as $row){
$username = $row['username'];
}
change to
foreach ($results->result() as $row) {
$username = $row->username;
}
Hope this tutorial can be useful to you when coding in CodeIgniter Framework
Reference :
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+4.9k Linux/MacOSX : Search for files by filename and extension with find command
+21.9k Find and replace a character in a string in Go
+7.1k How to show different content from website server when AdBlock is detected?
+17.2k Golang : Padding data for encryption and un-padding data for decryption
+8.9k Golang : Get escape characters \u form from unicode characters
+4.7k Golang : How to search a list of records or data structures
+15k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+9.9k How to test Facebook App on localhost ?
+10.6k Golang : convert(cast) float to string
+9.3k Golang : Simple File Server
+6.3k Golang : Create zip/ePub file without compression(use Store algorithm)
+17.8k Golang : Get current URL example