Get total number of rows when using LIMIT

前端 未结 2 967
旧巷少年郎
旧巷少年郎 2021-02-14 20:03

Been following examples from here and many other sites but not getting the expected results

Snippet of my PHP code;

$query=\"SELECT * FROM book\";
$resul         


        
相关标签:
2条回答
  • 2021-02-14 20:47

    Add a SQL_CALC_FOUND_ROWS to your select statement: http://dev.mysql.com/doc/refman/5.5/en/information-functions.html#function_found-rows

    This forces MySQL to count the rows as if the limit statement is not present.

    $query="SELECT SQL_CALC_FOUND_ROWS * FROM book LIMIT 0,4";
    
    0 讨论(0)
  • 2021-02-14 20:57

    Your most recent query returned 4 rows ($query="SELECT * FROM book LIMIT 0,4";).

    That's why it returns 4 and not 14.


    To quote the documentation:

    In the absence of the SQL_CALC_FOUND_ROWS option in the most recent successful SELECT statement, FOUND_ROWS() returns the number of rows in the result set returned by that statement. If the statement includes a LIMIT clause, FOUND_ROWS() returns the number of rows up to the limit.

    0 讨论(0)
提交回复
热议问题