4

我在 Symfony2 上使用 dbal 从我的表中检索一些信息:

$social = $conn->fetchAll('SELECT * FROM page WHERE  brand_id = :brand LIMIT :start,:limit', array('brand'=>$brand, 'start'=>(int) $start, 'limit'=>(int) $limit));

仅当我添加最后一部分(LIMIT ....)时才出现错误,这让我认为我无法限制 sql 查询内的结果,但在外部使用某种命令。我该如何解决这个问题?

错误:

An exception occurred while executing 'SELECT * FROM page WHERE brand_id = :brand LIMIT :start,:limit' with params {"brand":1,"start":0,"limit":4}:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''0','4'' at line 1
4

2 回答 2

9
$statement = $connection->prepare(
    'SELECT ... LIMIT :limit'
);
$statement->bindValue('limit', $limit, \PDO::PARAM_INT);
$statement->execute();

$result = $statement->fetchAll();
于 2014-01-14T15:58:19.263 回答
0

或者您可以像这样简单地使用 fetchAll($sql, array $params = array(), $types = array()) 中的第三个参数:

$social = $conn->fetchAll('SELECT * FROM page WHERE  brand_id = :brand LIMIT :start,:limit', array('brand'=>$brand, 'start'=>(int) $start, 'limit'=>(int) $limit), array(\PDO::PARAM_STR, \PDO::PARAM_INT, \PDO::PARAM_INT));
于 2017-04-27T14:21:24.727 回答