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
+37.9k Golang : UDP client server read write example
+9.6k Golang : Generate random elements without repetition or duplicate
+5.6k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+16.7k Golang : Check if a directory exist or not
+12.8k Golang : concatenate(combine) strings
+12.7k Golang : Simple word wrap or line breaking example
+33.5k Golang : Save image to PNG, JPEG or GIF format.
+7k Prevent Write failed: Broken pipe problem during ssh session with screen command
+5k Golang : Experimenting with the Rejang script
+9.7k Golang : Web routing/multiplex example
+6.4k Golang : Individual and total number of words counter example
+11.9k Golang : Generate Code128 barcode