Search code examples
phpmysqlilimitbind

MySqli limit not binding to prepared statement


I have made the following prepared query. It works perfectly if I just insert numbers manually where the ? are.

However, if I bind the parameters the query does not appear to run. How can I make it so that I can bind the limit numbers?

    if ($statement = $db -> prepare("SELECT blog_id, account_id, title, creation_time, body, timestamp 
    FROM blogs 
    ORDER BY creation_time DESC 
    LIMIT ?,?"))
    {
        $statement -> bind_param("ii", 2, 4);
        $statement -> execute();
        $statement -> store_result();
    }

Solution

  • When using bind_param(), you do not give the values directly, but variables that hold them. So what you need to do is:

    $offset = 2;
    $limit = 4;
    $statement->bind_param('ii', $offset, $limit);