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
+14.1k Golang : Parsing or breaking down URL
+8.6k Golang : Take screen shot of browser with JQuery example
+12.3k Golang : Exit, terminating or aborting a program
+5.6k Golang : Launching your executable inside a console under Linux
+16.3k Golang : File path independent of Operating System
+16.5k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+16.8k Golang : XML to JSON example
+18.4k Unmarshal/Load CSV record into struct in Go
+5.9k Golang : Extract XML attribute data with attr field tag example
+5.8k Linux/MacOSX : Search for files by filename and extension with find command
+17.7k Golang : Get all upper case or lower case characters from string example
+11.4k Golang : Surveillance with web camera and OpenCV