How to get the total found rows without considering the limit in PDO?

前端 未结 1 1917
广开言路
广开言路 2020-12-03 17:53

I found it very unprofessional to make a secondary query to check for more results in order to display a load more button.

I\'m using PDO, is there any way

相关标签:
1条回答
  • 2020-12-03 18:29

    MySQL only AFAIK:

    $r=$db->prepare("SELECT SQL_CALC_FOUND_ROWS * FROM locations WHERE area=:area LIMIT $start,10");
    $r->execute($fields);
    
    var_dump($r->fetchAll());
    
    var_dump($db->query('SELECT FOUND_ROWS();')->fetch(PDO::FETCH_COLUMN));
    

    About as heavy for the database server as querying a single time for all the records of course. For non-MySQL use, this query is of course better then getting the rowcount of all the records:

    $r=$db->prepare("SELECT COUNT(*) FROM locations WHERE area=:area");
    $r->execute($fields);
    $count = $r->fetch(PDO::FETCH_COLUMN);
    echo $count;
    
    0 讨论(0)
提交回复
热议问题