有很多相互矛盾的说法。在PHP中使用PDO获得行数的最佳方法是什么?在使用PDO之前,我只是简单地使用了mysql_num_rows。

fetchAll是我不想要的东西,因为我有时可能要处理大型数据集,所以不适合我使用。

你有什么建议吗?


当前回答

我尝试$count = $stmt->rowCount();在Oracle 11.2中,它没有工作。 我决定使用一个for循环,如下所示。

   $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;

其他回答

最简单的方法,只有两行,

$sql = $db->query("SELECT COUNT(*) FROM tablename WHERE statement='condition'");
echo $sql->fetchColumn();

有一个简单的解决办法。如果你使用PDO连接到你的DB,像这样:

try {

    $handler = new PDO('mysql:host=localhost;dbname=name_of_your_db', 'your_login', 'your_password'); 
    $handler -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch (PDOException $e) { 

    echo $e->getMessage();
}

现在,如果你想知道你的表中有多少行,并且你有列'id'作为主键,对DB的查询将是:

$query = $handler->query("SELECT id FROM your_table_name");

最后,要获得与查询匹配的行数,可以这样写:

$amountOfRows = $query->rowCount();

或者你可以这样写:

$query = $handler ->query("SELECT COUNT(id) FROM your_table_name");

$amountOfRows = $query->rowCount();

或者,如果你想知道表'products'中有多少产品的价格在10到20之间,写这样的查询:

$query = $handler ->query("SELECT id FROM products WHERE price BETWEEN 10 AND 
20");

$amountOfRows = $query->rowCount();

我尝试$count = $stmt->rowCount();在Oracle 11.2中,它没有工作。 我决定使用一个for循环,如下所示。

   $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;

当它是mysql的问题,如何计算或获得多少行在一个表与PHP PDO我使用这个

// 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'];

感谢Mike @ codeofaninja.com

$sql = "SELECT count(*) FROM `table` WHERE foo = ?"; 
$result = $con->prepare($sql); 
$result->execute([$bar]); 
$number_of_rows = $result->fetchColumn(); 

这不是最优雅的方式,而且它涉及到一个额外的查询。

PDO有PDOStatement::rowCount(),这显然在MySql中不起作用。真痛苦。

来自PDO文档:

对于大多数数据库, PDOStatement::rowCount()没有 返回受影响的行数 SELECT语句。相反,使用 PDO::query()发出SELECT COUNT(*)语句 谓词作为您想要的SELECT 语句,然后使用 PDOStatement: fetchColumn () 检索需要的行数 被归还。您的应用程序可以 请执行正确的操作。

编辑:上面的代码示例使用了一个预处理语句,在许多情况下,这对于计数行的目的可能是不必要的,因此:

$nRows = $pdo->query('select count(*) from blah')->fetchColumn(); 
echo $nRows;