简体   繁体   中英

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?

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...

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. 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.

Take a look at rowcount in PDO.

1) Useful for delete, update or insert queries

2) See the answer of kokx for select queries

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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