Returns the number of affected rows for INSERT, UPDATE or DELETE where supported.
The objective is to understand the behavior and operational limits, not only the method name.
What It Does
Returns the number of affected rows for INSERT, UPDATE or DELETE where supported.
Syntax and Return Value
PDOStatement::rowCount(): mixed
The return value and exceptional cases depend on the call described here: For SELECT, rowCount behavior is driver-dependent; use COUNT(*) instead.
Example 1: Basic Use
The statement has a real query and result set; database-specific metadata and statement options may be unsupported.
<?php
$pdo = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->exec('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, price INTEGER NOT NULL, active INTEGER)');
$pdo->exec("INSERT INTO products (id,name,price,active) VALUES (1,'Desk',120,1),(2,'Lamp',35,1),(3,'Chair',85,0)");
$stmt = $pdo->prepare("UPDATE products SET active = 1 WHERE id = ?");
$stmt->execute([1]);
echo $stmt->rowCount();
Example 2: A Second Scenario
The statement has a real query and result set; database-specific metadata and statement options may be unsupported.
<?php
$pdo = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->exec('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, price INTEGER NOT NULL, active INTEGER)');
$pdo->exec("INSERT INTO products (id,name,price,active) VALUES (1,'Desk',120,1),(2,'Lamp',35,1),(3,'Chair',85,0)");
$stmt = $pdo->prepare("UPDATE products SET active = 1 WHERE id = ?");
$stmt->execute([2]);
echo $stmt->rowCount();
Example 3: Practical Checks
The statement has a real query and result set; database-specific metadata and statement options may be unsupported.
<?php
$pdo = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->exec('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, price INTEGER NOT NULL, active INTEGER)');
$pdo->exec("INSERT INTO products (id,name,price,active) VALUES (1,'Desk',120,1),(2,'Lamp',35,1),(3,'Chair',85,0)");
$stmt = $pdo->prepare("UPDATE products SET active = 1 WHERE id = ?");
$stmt->execute([3]);
echo $stmt->rowCount();
Important Behavior and Edge Cases
For SELECT, rowCount behavior is driver-dependent; use COUNT(*) instead.
For production use, also account for error handling, data validation, resource limits and environment-specific driver behavior.
Comparison with Related Approaches
| Approach | Important distinction |
|---|---|
| PDOStatement::rowCount | Returns the number of affected rows for INSERT, UPDATE or DELETE where supported. |
| Related API / pattern | columnCount returns result column count, not affected row count. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Assuming the happy path covers edge cases | For SELECT, rowCount behavior is driver-dependent; use COUNT(*) instead. |
| Using a related API interchangeably | columnCount returns result column count, not affected row count. |
Key Takeaways
- Returns the number of affected rows for INSERT, UPDATE or DELETE where supported.
- For SELECT, rowCount behavior is driver-dependent; use COUNT(*) instead.
- columnCount returns result column count, not affected row count.
Choose this API or pattern when its exact semantics match your task, and verify the result against the database driver and runtime used by your application.