简体   繁体   English

PHP / MySQL-PDO库获取总行数

[英]PHP/MySQL - PDO Library Get Total Number of Rows

How would I go about getting the total number of rows in a table using the PDO library? 如何使用PDO库获取表中的总行数?

Below is how I execute my queries. 以下是我如何执行查询。

$query = " 

"; 

try 
{ 

    $stmt = $db->prepare($query); 
    $stmt->execute(); 
} 
catch(PDOException $ex) 
{ 

    die("Failed to run query: " . $ex->getMessage()); 
}

EDIT 编辑

I should have made it more clear, I'm sorry, but I was looking for the PDO way of getting number of rows... 对不起,我应该更清楚地说明这一点,但是我一直在寻找获取行数的PDO方法...

A database does not have rows. 数据库没有行。 A table, however, does. 但是,有一个表。 What I think that you are looking for, is a SQL query that will count the rows in a table. 我认为您正在寻找的是一个SQL查询,该查询将对表中的行进行计数。 Which you can do like this: 您可以这样做:

SELECT COUNT(*) AS count FROM table

If you execute that query, the count field in the result will contain the number of rows in the table. 如果执行该查询,结果中的count字段将包含表中的行数。

Take a look at rowcount in PDO. 看一下PDO中的行

1) Useful for delete, update or insert queries 1)对删除,更新或插入查询很有用

2) See the answer of kokx for select queries 2)有关选择查询,请参见kokx的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM