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
+16.4k Golang : Aligning strings to right, left and center with fill example
+21.7k Golang : Gorilla mux routing example
+8.2k Golang : Terminate-stay-resident or daemonize your program?
+7.6k Golang : Set or add headers for many or different handlers
+8.4k Golang : Copy map(hash table) example
+4.6k Golang : Get S3 or CloudFront object or file information
+4.8k Unix/Linux : How to test user agents blocked successfully ?
+4.4k Golang : Detect words using using consecutive letters in a given string
+2.8k Java : Random alphabets, alpha-numeric or numbers only string generator
+13.6k Golang : Accurate and reliable decimal calculations
+7.6k Golang : Combine slices but preserve order example
+18.3k Golang : Determine if directory is empty with os.File.Readdir() function