1

Possible Duplicate:
PHP PDO - Num Rows

I am working with PDO and MySQL, and I am fairly new to PDO.

So Have two questions?

  1. What function do I use to count the number of rows found based on a select?
  2. What function do I use to count the number of rows affected by an insert/delete/update?

According to the documentation it says:

If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behavior is not guaranteed for all databases and should not be relied on for portable applications.

That being said what should I use for these?

1
  • @nickb thanks, that pretty much answers my question. Commented Jan 12, 2013 at 22:13

3 Answers 3

4
  1. To guarantee a correct number of rows based on a select, you will need to use a PDO::query() with a SELECT COUNT(*) statement. Then use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned.
  2. For insert/update/delete, you can use PDOStatement::rowCount()

For more information, take a look at the PHP manual

Sign up to request clarification or add additional context in comments.

Comments

2

$stmt->rowCount() will return the number of rows returned by the query.

Comments

0

The function required would be rowCount(). Usage here.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.