I am having the following problem:
Environment PHP, codeigniter framework with PDO.
my code
public function index(){
$sql = "SELECT ........ LIMIT ?,?";
$q = $this->db->prepare($sql);
$limit = 0;
$offset = 10;
$q->execute(array($limit, $offset));
$r = $q->fetch();
var_dump($r);
}
problem: There is no problem in the sql query as, if i hardcode any integers (e.g. 0, 10) in the LIMIT clause; the query returns the desirable result. The problem occurs only when binding the params.
Error shown dumping the $r in above code returns bool(false)
Now, the following code runs successfully,
public function index(){
$sql = "SELECT ........ LIMIT 0,10";
$q = $this->db->prepare($sql);
$q->execute();
$r = $q->fetch();
var_dump($r);
}
what can be the problem in the former code while binding? Am i doing it wrongly or could there be chance that it is creeping up due to codeigniter not supporting PDO by default?
As adviced, I am putting my own answer.
You can’t bind variables to LIMIT or ORDER by clause
SOLUTION:
Pass it as a variable in the sql query (And probably sanitize with intval just to be safer.)