8

I want to fetch only one row from a database because I only expect one. However, with fetchAll I always have to unwrap the array first before I can access the meat:

$result = self::$db->fetchAll($select);
$result = $result[0];

Is there a better solution?

0

2 Answers 2

14

You can also use the fetchRow method, i.e.:

$result = self::$db->fetchRow($select);
// note that $result is a single object, not an array of objects

now you can access column name like this

  $myResult = $result->columnName;

See http://framework.zend.com/manual/1.11/en/zend.db.adapter.html#zend.db.adapter.select.fetchrow

Sign up to request clarification or add additional context in comments.

1 Comment

Well, thats even better. Thanks.
6

You can use the fetch method. Try this:

$result = self::$db->query($select)->fetch();

Reference: http://framework.zend.com/manual/en/zend.db.statement.html#zend.db.statement.fetching.fetch

2 Comments

Call to undefined method Zend_Db_Adapter_Pdo_Mysql::fetch()
Try it now. You have to call fetch on a query object, not a database adapter.

Your Answer

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.