Welcome Guest, Not a member yet? Register   Sign In
Query problem
#1

[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
#2

[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?
#3

[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’];
  }
#4

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




Theme © iAndrew 2016 - Forum software by © MyBB