PDOStatement::rowCount()
Also Known As
PDO rowCount
rows affected PHP
PDOStatement rowCount
TL;DR
Returns the number of rows affected by the last DELETE, INSERT, or UPDATE — unreliable for SELECT.
Explanation
rowCount() returns an integer reflecting rows modified by DML statements. For SELECT queries, the behaviour is driver-dependent — MySQL returns 0, making it unsuitable for counting result rows. Use SELECT COUNT(*) or fetchAll() + count() to count SELECT results. rowCount() is useful for detecting whether an UPDATE actually changed a row vs matched but left it unchanged.
Watch Out
⚠ rowCount() on SELECT returns 0 on most drivers even when rows exist. The PHP manual explicitly marks this behaviour as not portable.
Common Misconception
✗ rowCount() works reliably for SELECT queries. The PHP manual explicitly warns it is not portable for SELECT — use COUNT(*) or check if fetch() returns false instead.
Why It Matters
Developers often use rowCount() to check if a SELECT returned results — this works on MySQL by accident but breaks on other databases, causing subtle portability bugs.
Common Mistakes
- Using rowCount() to check if a SELECT found results — returns 0 on many drivers even with results.
- Expecting rowCount() to return rows matched — it returns rows changed. UPDATE with identical values returns 0.
- Not checking rowCount() after DELETE to confirm a row was actually removed.
Avoid When
- Never use rowCount() to check if a SELECT returned results — use fetch() === false or count(fetchAll()) instead.
When To Use
- Use after DELETE to verify a row was actually removed.
- Use after UPDATE to distinguish 'row not found' from 'row found but value unchanged'.
Code Examples
✗ Vulnerable
// Wrong — rowCount() on SELECT is not portable
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = ?');
$stmt->execute([$email]);
if ($stmt->rowCount() > 0) { // unreliable — may return 0 even with results
$user = $stmt->fetch();
}
✓ Fixed
// Correct: count SELECT results
$stmt = $pdo->prepare('SELECT * FROM users WHERE active = 1');
$stmt->execute();
$users = $stmt->fetchAll();
$count = count($users); // reliable
// Correct: check rows affected by UPDATE
$stmt = $pdo->prepare('UPDATE users SET last_seen = NOW() WHERE id = ?');
$stmt->execute([$userId]);
if ($stmt->rowCount() === 0) {
// User not found or value unchanged
}
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
31 Mar 2026
Views
17
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Perplexity 6
Unknown AI 4
Google 1
ChatGPT 1
Meta AI 1
Ahrefs 1
Also referenced
How they use it
crawler 13
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
⚙ Fix effort: Low
⚡ Quick Fix
Use fetchAll() + count() or SELECT COUNT(*) to count SELECT rows — use rowCount() only for INSERT/UPDATE/DELETE
📦 Applies To
PHP 5.1+
web
cli
🔗 Prerequisites
🔍 Detection Hints
rowCount() used after SELECT statement
Auto-detectable:
✓ Yes
phpstan
🤖 AI Agent
Confidence: High
False Positives: Low
✓ Auto-fixable
Fix: Low
Context: Line