Row count with PDO

前端 未结 23 3667
春和景丽
春和景丽 2020-11-21 22:57

There are many conflicting statements around. What is the best way to get the row count using PDO in PHP? Before using PDO, I just simply used mysql_num_rows.

23条回答
  •  野的像风
    2020-11-21 23:20

    You can combine the best method into one line or function, and have the new query auto-generated for you:

    function getRowCount($q){ 
        global $db;
        return $db->query(preg_replace('/SELECT [A-Za-z,]+ FROM /i','SELECT count(*) FROM ',$q))->fetchColumn();
    }
    
    $numRows = getRowCount($query);
    

提交回复
热议问题