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
+15.6k Golang : Get input from keyboard
+5.1k AWS S3 : Prevent Hotlinking policy
+16.5k Golang : Read data from config file and assign to variables
+16k Golang : How to save log messages to file?
+10.9k Golang : Flush and close file created by os.Create and bufio.NewWriter example
+7.8k Golang : Find network service name from given port and protocol
+14.7k Golang : Update database with GORM example
+11.3k Golang : Sort and reverse sort a slice of bytes
+5.8k Restart Apache or Nginx web server without password prompt
+8.9k Golang : Channels and buffered channels examples
+18.7k Golang : Accept input from user with fmt.Scanf skipped white spaces and how to fix it
+5.2k Javascript : Generate random key with specific length