-1

Hello I was building my Query class which I saw on YouTube, but I'm stuck. My Query function that allows you to use advanced SQL queries like SELECT * FROM market LIMIT ? OFFSET ? It binds values so I can't find any solution. Anybody help? What should I do?

My Query.php class contains

public function query($sql, $params = array())
{
    $this->_error = false;

    if ($this->_query = $this->db->prepare($sql))
    {
        $x = 1;

        if (count($params))
        {
            foreach ($params as $param)
            {
                $this->_query->bindValue($x, $param);
                $x++;
            }
        }

        if ($this->_query->execute())
        {
            $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
            $this->_count = $this->_query->rowCount();
        } else {
            $this->_error = true;
        }
    }

    return $this;
}

Here I tried to select items from db like it's in my Query bellow

$i = 3;
$x = 100;
$sql = Query::getInstance()->query("SELECT * FROM market LIMIT ? OFFSET ?", array($i, $x));

var_dump($sql);

I didn't put here full source code, I think there is a problem in query function but I'm not able to find it.

ERROR IMAGE enter image description here

krlzlx
  • 5,752
  • 14
  • 47
  • 55
Jakub Staněk
  • 1,023
  • 1
  • 10
  • 11
  • http://stackoverflow.com/questions/2269840/how-to-apply-bindvalue-method-in-limit-clause –  Apr 11 '17 at 21:53
  • And there are more than 100 entries in the database? It seems the query runs - it seems it returns bool(true), which, if I understand that mess of a var_dump() correctly, that the query runs. – junkfoodjunkie Apr 11 '17 at 21:54
  • But there is problem that error = bool(true) it means that something went wrong, it seems like executing the query – Jakub Staněk Apr 11 '17 at 21:59
  • Run `SELECT * FROM market LIMIT 3 OFFSET 100;` directly in your database and tell us what error you are getting. p.s. `Query` is probably not a good name for your database connection -- not intuitive. – mickmackusa Apr 11 '17 at 22:08
  • It actually works, the problem was PDO::ATTR_EMULATE_PREPARES – Jakub Staněk Apr 11 '17 at 22:11
  • Possible duplicate of [Php Prepared Statements Turn Emulation Off](http://stackoverflow.com/questions/15718148/php-prepared-statements-turn-emulation-off) – mickmackusa Apr 11 '17 at 22:12
  • 1
    There are many SO questions that answer this question, I flagged using just one. In the future, exhaustively search SO before posting a question, because the chances are: someone has already asked your question and someone else has already answered it. SO is a wonderful resource, please use it to the fullest. – mickmackusa Apr 11 '17 at 22:15

1 Answers1

0

Thanks nogad, All i do is set this at the beginning of query function:

$this->db->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );
Jakub Staněk
  • 1,023
  • 1
  • 10
  • 11