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.8k Golang : Perform sanity checks on filename example
+19.1k Golang : Get current URL example
+14.3k Golang : How to get URL port?
+8.2k Golang : How to check variable or object type during runtime?
+18.9k Golang : Check if directory exist and create if does not exist
+36.2k Golang : Display float in 2 decimal points and rounding up or down
+13.7k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+19.5k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+29.2k Golang : Login(Authenticate) with Facebook example
+14.3k Golang : Convert(cast) int to float example
+26.3k Golang : Encrypt and decrypt data with AES crypto
+20.1k Golang : Pipe output from one os.Exec(shell command) to another command