PHP-MySQL issue with LIMIT clause

Viewed 30
require 'conn.php';
$cardPerRow = isset($_GET['rpp']) ? $_GET['rpp'] : 9;
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$offset = $cardPerRow * ($page-1);
var_dump($offset);var_dump($cardPerRow);
        $query = "SELECT * FROM dbname.prodotti LIMIT ? OFFSET ?;";
        $stmt = $conn->prepare($query);
        //$stmt->bindParam(':offset',$offset);
        //$stmt-> bindParam(':cardPerRow',$cardPerRow);
        $stmt ->execute([$cardPerRow,$offset]);
        $data = $stmt ->fetchAll();
        echo json_encode($data);

This code return an empty array, but if i run the query on my database it returns my data. I really don't know why, for me everything is good. Is there something that i don't see? Thanks in advance.

1 Answers

You cannot use bind parameters for the numbers in LIMIT and OFFSET clauses. You must embed those numbers directly in the text of your query.

And, if you had checked for errors that would have been clear. Please read this. Turning query errors to Exceptions in MySQLi

Related