开发者

SQL LIMIT return 'zero' - 0 - rows(IN PHP)

开发者 https://www.devze.com 2023-01-09 17:43 出处:网络
I have an error in this query, when the query returns zero rows. Error Number: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right

I have an error in this query, when the query returns zero rows.

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 '-20,20' at line 3

SELECT pl.name,pl.email FROM players pl JOIN players_bonus pl_b on pl.id = pl_b.id_player WHERE pl_b.id_bonus = 3 LIM开发者_如何学编程IT -20,20

My method is:


    public function getPViews_num_rows($limit = array(0,20),$page_num = 1,$id) {
        $limit = "LIMIT {$limit[0]},{$limit[1]}";

    $sql = "SELECT pl.name,pl.email FROM players pl 
            JOIN players_bonus pl_b on pl.id = pl_b.id_player 
                WHERE pl_b.id_bonus = ? {$limit}";

    $where =  array($id);
    $query = $this->db->query ( $sql,$where );
    return $query->num_rows ();
}

I don't wanna do another query to count rows,before I do this query. Thanks for the answers.


The OFFSET value of the LIMIT clause has to be a nonnegative integer constant. Quoting the MySQL Documentation:

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).


You can not specify negative value to LIMIT clause:

LIMIT -20,20 

It means you want to return 20 rows starting from -20 which is wrong.


The limit cannot be negative.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号