-2

我有以下内容:

public function getAll($limit = 100)
{   
    //if ($thread != 0) { $threadq = "WHERE threadId=$threadId"; }

    $query = <<<EOF
    SELECT 
        x.*
    FROM x

    ORDER BY dater DESC
    LIMIT ?
EOF;
    return self::$db->fetchAll($query, $limit);
}   

似乎它变成了LIMIT xLIMIT 'x'因此 MySQL 查询解析器出错并抱怨。

但是做得LIMIT $limit很好

为什么这不起作用?还有另一种方法吗?

4

2 回答 2

4

替换参数必须在数组内,即使只有一个:

return self::$db->fetchAll($query, array($limit));

对于您不需要使用的查询的限制部分?替换通配符!

于 2012-08-09T18:15:02.090 回答
2

我要疯了,建议你保持简单并使用Zend_Db_Select。这似乎是一个简单的查询。

这个演示使用默认的ZF DbTable作为适配器(我可以很容易地使用Zend_Db_Table::getDefaultAdapter();),但是它可以适应几乎任何类型的查询:

<?php

class Application_Model_DbTable_User extends Zend_Db_Table_Abstract
{
    protected $_name = 'user';
    protected $_primary = 'id';

public function getAll($limit = 100)
{
    $select = $this->select();//This is the adapter for this table and implies SELECT * FROM user 
    $select->order('name', 'DESC') //can also pass an array of columns
           ->limit($limit);//limit has a second arg for offset

    return $this->fetchAll($select);//returns a rowset object, if an array is required append ->toArray to the fetchall().
}   
}
于 2012-08-10T16:56:33.783 回答