Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fatal error: Cannot use object of type stdClass as array in

I'm getting the error:

"Fatal error: Cannot use object of type stdClass as array in" on line 183

From this code:

$getvidids = $ci->db->query(     "SELECT * FROM videogroupids " .      "WHERE videogroupid='$videogroup' AND used='0' LIMIT 10");  foreach ($getvidids->result() as $row){     $vidid = $row['videoid'];              //This is line 183 } 

Anyone know what's wrong with the above code? Or what this error means?

like image 785
kyle Avatar asked Mar 23 '11 23:03

kyle


People also ask

What is StdClass object in PHP?

The stdClass is the empty class in PHP which is used to cast other types to object. It is similar to Java or Python object. The stdClass is not the base class of the objects. If an object is converted to object, it is not modified.

How do you find the value of the StdClass object?

You create StdClass objects and access methods from them like so: $obj = new StdClass; $obj->foo = "bar"; echo $obj->foo; I recommend subclassing StdClass or creating your own generic class so you can provide your own methods. Thank you for your help!


2 Answers

CodeIgniter returns result rows as objects, not arrays. From the user guide:

result()


This function returns the query result as an array of objects, or an empty array on failure.

You'll have to access the fields using the following notation:

foreach ($getvidids->result() as $row) {     $vidid = $row->videoid; } 
like image 83
BoltClock Avatar answered Oct 09 '22 10:10

BoltClock


if you really want an array instead you can use:

$getvidids->result_array() 

which would return the same information as an associative array.

like image 40
icchanobot Avatar answered Oct 09 '22 09:10

icchanobot