PDO rowCount()
rowCount() returns rows affected by INSERT/UPDATE/DELETE. For SELECT, it may return the fetched count but is unreliable — use COUNT(*) in SQL instead.
rowCount() returns rows affected by INSERT/UPDATE/DELETE. For SELECT, it may return the fetched count but is unreliable — use COUNT(*) in SQL instead.
<?php
// After UPDATE or DELETE
$stmt = $pdo->prepare("DELETE FROM cache WHERE expires_at < NOW()");
$stmt->execute();
$deleted = $stmt->rowCount();
echo "Deleted {$deleted} expired cache entries";
// After INSERT
$stmt = $pdo->prepare("INSERT INTO notifications (user_id, message) VALUES (?, ?)");
$stmt->execute([$userId, $message]);
echo "Inserted " . $stmt->rowCount() . " row";
// For SELECT count — use SQL COUNT, not rowCount()
$count = $pdo->query("SELECT COUNT(*) FROM users")->fetchColumn();
Never rely on rowCount() for SELECT statements — use SELECT COUNT(*) for reliable row counts.