使用Zend DB框架从MySQL中只抓取一行


Fetching only one row from MySQL using Zend DB framework

我只想从数据库中获取一行,因为我只期望一行。然而,对于fetchAll,我总是必须先打开数组,然后才能访问肉:

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

有更好的解决方案吗?

您也可以使用fetchRow方法,即:

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

现在您可以像这样访问列名

  $myResult = $result->columnName;

看到http://framework.zend.com/manual/1.11/en/zend.db.adapter.html zend.db.adapter.select.fetchrow

可以使用fetch方法。试试这个:

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

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