{
    "slug": "pdo_rowcount",
    "term": "PDOStatement::rowCount()",
    "category": "php",
    "difficulty": "beginner",
    "short": "Returns the number of rows affected by the last DELETE, INSERT, or UPDATE — unreliable for SELECT.",
    "long": "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.",
    "aliases": [
        "PDO rowCount",
        "rows affected PHP",
        "PDOStatement rowCount"
    ],
    "tags": [
        "php",
        "database",
        "pdo"
    ],
    "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."
    ],
    "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'."
    ],
    "avoid_when": [
        "Never use rowCount() to check if a SELECT returned results — use fetch() === false or count(fetchAll()) instead."
    ],
    "related": [
        "pdo",
        "pdo_fetch_modes",
        "prepared_statement"
    ],
    "prerequisites": [
        "pdo"
    ],
    "refs": [
        "https://www.php.net/manual/en/pdostatement.rowcount.php"
    ],
    "bad_code": "// Wrong — rowCount() on SELECT is not portable\n$stmt = $pdo->prepare('SELECT * FROM users WHERE email = ?');\n$stmt->execute([$email]);\nif ($stmt->rowCount() > 0) { // unreliable — may return 0 even with results\n    $user = $stmt->fetch();\n}",
    "good_code": "// Correct: count SELECT results\n$stmt = $pdo->prepare('SELECT * FROM users WHERE active = 1');\n$stmt->execute();\n$users = $stmt->fetchAll();\n$count = count($users); // reliable\n\n// Correct: check rows affected by UPDATE\n$stmt = $pdo->prepare('UPDATE users SET last_seen = NOW() WHERE id = ?');\n$stmt->execute([$userId]);\nif ($stmt->rowCount() === 0) {\n    // User not found or value unchanged\n}",
    "quick_fix": "Use fetchAll() + count() or SELECT COUNT(*) to count SELECT rows — use rowCount() only for INSERT/UPDATE/DELETE",
    "effort": "low",
    "created": "2026-03-31",
    "updated": "2026-03-31",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/pdo_rowcount",
        "html_url": "https://codeclaritylab.com/glossary/pdo_rowcount",
        "json_url": "https://codeclaritylab.com/glossary/pdo_rowcount.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[PDOStatement::rowCount()](https://codeclaritylab.com/glossary/pdo_rowcount) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/pdo_rowcount"
            }
        }
    }
}