Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit sql result on dbal

Im using dbal on Symfony2 to retrieve some info from my table:

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

Im getting an error only when I add the last part (LIMIT ....), this make me think that i cant limit the result inside the sql query but outside using some kind of command. How can i solve this?

Error:

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
like image 528
DomingoSL Avatar asked Jan 13 '23 03:01

DomingoSL


1 Answers

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

$result = $statement->fetchAll();
like image 72
Aistis Avatar answered Jan 21 '23 21:01

Aistis