Row count with PDO

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

    When it is matter of mysql how to count or get how many rows in a table with PHP PDO I use this

    // count total number of rows
    $query = "SELECT COUNT(*) as total_rows FROM sometable";
    $stmt = $con->prepare($query);
    
    // execute query
    $stmt->execute();
    
    // get total rows
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    $total_rows = $row['total_rows'];
    

    credits goes to Mike @ codeofaninja.com

提交回复
热议问题