CodeIgniter Forums
Query problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Query problem (/showthread.php?tid=41748)



Query problem - El Forum - 05-16-2011

[eluser]veledrom[/eluser]
Hi,

I have a a problem I cannot find why it doesn't work. Error message is at the bottom:

This doesn't work:
Quote:public function get_users($num, $offset)
{
$sql = "SELECT * FROM login LIMIT ? OFFSET ?";
$data['dbquery'] = $this->db->query($sql, array($num, $offset));
return $data['dbquery'];
}


This works:
Quote:public function get_users($num, $offset)
{
$data['dbquery'] = $this->db->get('login', $num, $offset);
return $data['dbquery'];
}


Thanks in advance


ERROR:

A Database Error Occurred
Error Number: 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 ''2' OFFSET '4'' at line 1

SELECT * FROM login LIMIT '2' OFFSET '4'

Filename: D:\Programming\LocalServer\system\database\DB_driver.php

Line Number: 330



Query problem - El Forum - 05-16-2011

[eluser]Dan Storm[/eluser]
The problem is that you are missing a whitespace between <strong>LIMIT '2'</strong> and <strong>OFFSET '4'</strong>.

Are you certain that you have the missing whitespace in your code?


Query problem - El Forum - 05-16-2011

[eluser]eoinmcg[/eluser]
the problem is that $num and $offset are both strings and not integers. when sql binds the variables into the query it wraps strings with the backtick character, e.g. `2`. in your case this results in an invalid query

this will work
Code:
public function get_users($num, $offset)
  {

      $num = (int) $num;
      $offset = (int) $offset;

      $sql = “SELECT * FROM login LIMIT ? OFFSET ?”;
      $data[‘dbquery’] = $this->db->query($sql, array($num, $offset));
      return $data[‘dbquery’];
  }



Query problem - El Forum - 05-16-2011

[eluser]veledrom[/eluser]
Yesss, excellent. Thanks eoinmcg.