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
+6.8k Web : How to see your website from different countries?
+14.6k Golang : Submit web forms without browser by http.PostForm example
+19k Mac OSX : Homebrew and Golang
+9.1k Golang : How to get username from email address
+19.6k Golang : Accept input from user with fmt.Scanf skipped white spaces and how to fix it
+23k Golang : Print out struct values in string format
+9.1k Golang : Temperatures conversion example
+11.9k Golang : Pagination with go-paginator configuration example
+8k Golang : Configure Apache and NGINX to access your Go service example
+9k Golang : Create and shuffle deck of cards example
+11.4k Golang : Convert(cast) float to int
+15.8k Golang : Get sub string example