Row count with PDO

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

    fetchColumn()

    used if want to get count of record [effisien]

    $sql   = "SELECT COUNT(*) FROM fruit WHERE calories > 100";
    $res   = $conn->query($sql);
    $count = $res->fetchColumn(); // ex = 2
    

    query()

    used if want to retrieve data and count of record [options]

    $sql = "SELECT * FROM fruit WHERE calories > 100";
    $res = $conn->query($sql);
    
    if ( $res->rowCount() > 0) {
    
        foreach ( $res as $row ) {
            print "Name: {$row['NAME']} <br />";
        }
    
    }
    else {
        print "No rows matched the query.";
    }
    

    PDOStatement::rowCount

    0 讨论(0)
  • 2020-11-21 23:26

    As I wrote previously in an answer to a similar question, the only reason mysql_num_rows() worked is because it was internally fetching all the rows to give you that information, even if it didn't seem like it to you.

    So in PDO, your options are:

    1. Use PDO's fetchAll() function to fetch all the rows into an array, then use count() on it.
    2. Do an extra query to SELECT COUNT(*), as karim79 suggested.
    3. Use MySQL's FOUND_ROWS() function UNLESS the query had SQL_CALC_FOUND_ROWS or a LIMIT clause (in which case the number of rows that were returned by the query and the number returned by FOUND_ROWS() may differ). However, this function is deprecated and will be removed in the future.
    0 讨论(0)
  • 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. :)

    0 讨论(0)
  • 2020-11-21 23:28

    I tried $count = $stmt->rowCount(); with Oracle 11.2 and it did not work. I decided to used a for loop as show below.

       $count =  "";
        $stmt =  $conn->prepare($sql);
        $stmt->execute();
       echo "<table border='1'>\n";
       while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
            $count++;
            echo "<tr>\n";
        foreach ($row as $item) {
        echo "<td class='td2'>".($item !== null ? htmlentities($item, ENT_QUOTES):"&nbsp;")."</td>\n";
            } //foreach ends
            }// while ends
            echo "</table>\n";
           //echo " no of rows : ". oci_num_rows($stmt);
           //equivalent in pdo::prepare statement
           echo "no.of rows :".$count;
    
    0 讨论(0)
  • 2020-11-21 23:29

    I ended up using this:

    $result = $db->query($query)->fetchAll();
    
    if (count($result) > 0) {
        foreach ($result as $row) {
            echo $row['blah'] . '<br />';
        }
    } else {
        echo "<p>Nothing matched your query.</p>";
    }
    
    0 讨论(0)
  • 2020-11-21 23:29

    when you make a COUNT(*) in your mysql statement like in

    $q = $db->query("SELECT COUNT(*) FROM ...");
    

    your mysql query is already counting the number of result why counting again in php? to get the result of your mysql

    $q = $db->query("SELECT COUNT(*) as counted FROM ...");
    $nb = $q->fetch(PDO::FETCH_OBJ);
    $nb = $nb->counted;
    

    and $nb will contain the integer you have counted with your mysql statement a bit long to write but fast to execute

    Edit: sorry for the wrong post but as some example show query with count in, I was suggesting using the mysql result, but if you don't use the count in sql fetchAll() is efficient, if you save the result in a variable you won't loose a line.

    $data = $dbh->query("SELECT * FROM ...");
    $table = $data->fetchAll(PDO::FETCH_OBJ);
    

    count($table) will return the number of row and you can still use the result after like $row = $table[0] or using a foreach

    foreach($table as $row){
      print $row->id;
    }
    
    0 讨论(0)
提交回复
热议问题