Json_encode does nothing in my php

I have this little code, I know that I am getting a fine from the database because I did a few print_r and worked fine:

//build query SQL $query = $this ->select() ->where('numBedrooms=?',$numBedrooms) ->where('type=?',$type) ->where('state=?',$state) ->limit(8);//8 rows, with an offset of $recent_page*8-8 //execute query SQL $rows=$this->fetchAll($query); //encode json $var= json_encode($rows);//-------->var is empty always!! 
+4
source share
3 answers

If fetchAll () returns an object, as you say, then it would be wise to convert this to an array first and then pass it to json_encode. I do not believe json_encode works with objects.

0
source

You need to convert the string to an array:

$var= json_encode($rows->toArray());

See Getting a String as an Array at http://framework.zend.com/manual/en/zend.db.table.rowset.html

+4
source

To add another answer to this question ...

Zend provides various ways to access data. It's easier for me to work with arrays in Zend, as it makes your code more portable.

Using Zend_Db_Table_Abstract:

 class Model_MyStuff extends Zend_Db_Table_Absract { protected $_name = 'Stuff'; protected $_primary = 'StuffID'; function getStuff() { $select = $this->select(); $select->where('Active = 1'); $results = $select->query()->fetchAll(); if (count($results) > 0) return $results; return null; } } 

This code will return an array instead of objects that can be immediately passed to json_encode. The difference is that you are requesting a fetchAll ($ query) object, while I select select for the query () -> fetchAll (). I believe that for this you need to select a select object from $ this-> select ().

+3
source

All Articles