Row count with PDO

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

    This post is old but Getting row count in php with PDO is simple

    $stmt = $db->query('SELECT * FROM table');
    $row_count = $stmt->rowCount();
    
    0 讨论(0)
  • 2020-11-21 23:15

    Use parameter array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL), else show -1:

    Usen parametro array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL), sin ello sale -1

    example:

    $res1 = $mdb2->prepare("SELECT clave FROM $tb WHERE id_usuario='$username' AND activo=1 and id_tipo_usuario='4'", array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
    $res1->execute();
    
    $count=$res1->rowCount();
    echo $count;
    
    0 讨论(0)
  • 2020-11-21 23:17
    $sql = "SELECT count(*) FROM `table` WHERE foo = ?"; 
    $result = $con->prepare($sql); 
    $result->execute([$bar]); 
    $number_of_rows = $result->fetchColumn(); 
    

    Not the most elegant way to do it, plus it involves an extra query.

    PDO has PDOStatement::rowCount(), which apparently does not work in MySql. What a pain.

    From the PDO Doc:

    For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement. Instead, use PDO::query() to issue a SELECT COUNT(*) statement with the same predicates as your intended SELECT statement, then use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned. Your application can then perform the correct action.

    EDIT: The above code example uses a prepared statement, which is in many cases is probably unnecessary for the purpose of counting rows, so:

    $nRows = $pdo->query('select count(*) from blah')->fetchColumn(); 
    echo $nRows;
    
    0 讨论(0)
  • 2020-11-21 23:17

    Answering this because I trapped myself with it by now knowing this and maybe it will be useful.

    Keep in mind that you cant fetch results twice. You have to save fetch result into array, get row count by count($array), and output results with foreach. For example:

    $query = "your_query_here";
    $STH = $DBH->prepare($query);
    $STH->execute();
    $rows = $STH->fetchAll();
    //all your results is in $rows array
    $STH->setFetchMode(PDO::FETCH_ASSOC);           
    if (count($rows) > 0) {             
        foreach ($rows as $row) {
            //output your rows
        }                       
    }
    
    0 讨论(0)
  • 2020-11-21 23:17
    <table>
          <thead>
               <tr>
                    <th>Sn.</th>
                    <th>Name</th>
               </tr>
          </thead>
          <tbody>
    <?php
         $i=0;
         $statement = $db->prepare("SELECT * FROM tbl_user ORDER BY name ASC");
         $statement->execute();
         $result = $statement->fetchColumn();
         foreach($result as $row) {
            $i++;
        ?>  
          <tr>
             <td><?php echo $i; ?></td>
             <td><?php echo $row['name']; ?></td>
          </tr>
         <?php
              }
         ?>
         </tbody>
    </table>
    
    0 讨论(0)
  • 2020-11-21 23:18

    If you just want to get a count of rows (not the data) ie. using COUNT(*) in a prepared statement then all you need to do is retrieve the result and read the value:

    $sql = "SELECT count(*) FROM `table` WHERE foo = bar";
    $statement = $con->prepare($sql); 
    $statement->execute(); 
    $count = $statement->fetch(PDO::FETCH_NUM); // Return array indexed by column number
    return reset($count); // Resets array cursor and returns first value (the count)
    

    Actually retrieving all the rows (data) to perform a simple count is a waste of resources. If the result set is large your server may choke on it.

    0 讨论(0)
提交回复
热议问题