Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetching only one row from MySQL using Zend DB framework

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?

like image 754
Lenar Hoyt Avatar asked Aug 23 '11 12:08

Lenar Hoyt


2 Answers

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

like image 169
dinopmi Avatar answered Nov 02 '22 21:11

dinopmi


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

like image 6
Chris Laplante Avatar answered Nov 02 '22 21:11

Chris Laplante