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
+11.2k Golang : Search and extract certain XML data example
+20.2k SSL : How to check if current certificate is sha1 or sha2
+15.6k Golang : Get input from keyboard
+23.4k Golang : Create PDF file from HTML file
+9k Golang : Use regular expression to get all upper case or lower case characters example
+19.1k Golang : How to force compile or remove object files first before rebuild?
+6.5k Golang : What fmt.Println() can do and println() cannot do
+7k Golang : Append and add item in slice
+13.1k Golang : Recombine chunked files example
+3.9k Golang : Calculate a pip value and distance to target profit example
+9.1k Golang : Text file editor (accept input from screen and save to file)
+5.7k Golang : Muxing with Martini example