Row count with PDO

前端 未结 23 3160
春和景丽
春和景丽 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:27

    This is super late, but I ran into the problem and I do this:

    function countAll($table){
       $dbh = dbConnect();
       $sql = "select * from `$table`";
    
       $stmt = $dbh->prepare($sql);
        try { $stmt->execute();}
        catch(PDOException $e){echo $e->getMessage();}
    
    return $stmt->rowCount();
    

    It's really simple, and easy. :)

提交回复
热议问题