0

Any idea why this is returning false?

Code using to call function:

$product = new Product;
$allProducts = $product->getProducts(12);

Function I'm calling:

public function getProducts($limit)
    {
        $values = array($limit);
        $statement = $this->conn->prepare('SELECT * FROM sellify_items ORDER BY id DESC LIMIT ?');
        $statement->execute($values);
        $result = $statement->fetchAll();
        if ($result) {
            return $result;
        }
        return false;
    }

Edit: Updated function

public function getProducts($limit)
{
    $values = array(intval($limit));
    $statement = $this->conn->prepare('SELECT * FROM sellify_items ORDER BY id DESC LIMIT ?');
    $statement->bindValue(0, $limit, PDO::PARAM_INT);
    $statement->execute($values);
    $result = $statement->fetchAll();
    if ($result) {
        return $result;
    }
    return false;
}
meowfishcat
  • 137
  • 14
  • last time i had it, it was because my `dbname` in DSN was not good, did you checked your connection ? – Bobot May 03 '15 at 22:50
  • Connection works fine. if I remove the $values from the execute, and replace the ? with a number it works fine... – meowfishcat May 03 '15 at 22:51
  • 1
    @Cherryade You might want to take a look here: http://stackoverflow.com/q/2269840/3933332 – Rizier123 May 03 '15 at 22:52
  • OH WELL PDO just surround your var with `'` you should use `$stmt->bindValue(0, $limit, PDO::PARAM_INT);` ;) – Bobot May 03 '15 at 22:53

1 Answers1

1

Try this (note casting to int):

$statement->bindValue(0, (int) $limit, PDO::PARAM_INT);
Michal Przybylowicz
  • 1,558
  • 3
  • 16
  • 22