4

I've this small code, I know I receive fine from the database because I made some print_r and work 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!!
Danilo Valente
11.4k8 gold badges55 silver badges71 bronze badges
asked Jul 26, 2012 at 0:34
4
  • 2
    What does fetchAll() return? Commented Jul 26, 2012 at 0:34
  • an object, and inside has arrays and more objects Commented Jul 26, 2012 at 0:36
  • print_r gives me this result: snipt.org/vsfh0#expand Commented Jul 26, 2012 at 0:41
  • 2
    json_encode() will only encode public properties in objects. Commented Jul 26, 2012 at 0:42

3 Answers 3

4

You need to convert your row set to an array:

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

See Retrieving a Rowset as an Array on http://framework.zend.com/manual/en/zend.db.table.rowset.html

answered Jul 26, 2012 at 0:43
Sign up to request clarification or add additional context in comments.

2 Comments

This is the correct solution. Just to clarify: json_encode can serialize any PHP type (excluding resources). The OP's problem: Zend_Db_Table_Rowset::_data is protected and not directly accessible by json_encode or other functions. Hence the need to call toArray() to get at the protected data.
@powers1 Indeed. Thanks for contributing with additional details
3

To add a different answer to this...

Zend provides different ways of accessing data. I find it easier to always 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 which can immediately be passed into json_encode. The difference is you're asking the object to fetchAll($query) whereas I'm asing the select to query()->fetchAll(). I believe the select object needs to come from $this->select() for this to work however.

answered Sep 29, 2012 at 18:08

Comments

0

If fetchAll() is returning an object like you say it is, then it would be wise to first convert this to an array and then to pass it to json_encode. I do not believe json_encode work's with objects.

answered Jul 26, 2012 at 0:38

2 Comments

@Radiu: that doesn't mean it'll return what you might expect.
@Hamish, it will, as long as you expect to see the public properties.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.