How to bind queries that use variables for the LIMIT parameter in Codeigniter

393 Views Asked by At

I am trying to fix a query in codeigniter, by binding queries for safety. But I am unable to use "?" in the limit parameter of the sql query.

Without using codeigntier's query builder, how can I fix this problem and still enable the queries to be escaped/safe?

My CODE

$query = " SELECT * FROM users ORDER BY uid DESC LIMIT ?, ? ";

$bind  = array($one, $two) 
$query = $this->db->query($query, $bind);

The error I get is below

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?, ?' at line 1

Thanks

1

There are 1 best solutions below

3
Pradeep On

Hope this will help you :

Remove $id from your $bind , since u r not using it anywhere in your $query

$sql= " SELECT * FROM users ORDER BY uid DESC LIMIT ?, ? ";

$bind  = array($one, $two);
$query = $this->db->query($sql, $bind);

/* to test 
 echo $this->db->last_query();
*/

For more : https://www.codeigniter.com/user_guide/database/queries.html#query-bindings