diff --git a/reference/pdo/functions/PDOStatement-rowCount.xml b/reference/pdo/functions/PDOStatement-rowCount.xml index 74bd0703d6..1002d1e040 100644 --- a/reference/pdo/functions/PDOStatement-rowCount.xml +++ b/reference/pdo/functions/PDOStatement-rowCount.xml @@ -1,5 +1,5 @@ - + @@ -8,8 +8,8 @@ Returns the number of rows affected by the last SQL statement - - Description + + &reftitle.description; intPDOStatement::rowCount @@ -28,8 +28,17 @@ behaviour is not guaranteed for all databases and should not be relied on for portable applications. - Return the number of deleted rows - + + + &reftitle.examples; + + + Return the number of deleted rows + + PDOStatement::rowCount returns the number of + rows affected by a DELETE, INSERT, or UPDATE statement. + + rowCount(); print("Deleted $count rows.\n"); ?> ]]> - - + + &example.outputs; + + + + + + Counting rows returned by a SELECT statement + + For most databases, PDOStatement::rowCount does not + return the number of rows affected by a SELECT statement. Instead, use + PDO::query to issue a SELECT COUNT(*) statement + with the same predicates as your intended SELECT statement, then use + PDOStatement::fetchSingle to retrieve the number + of rows that will be returned. Your application can then perform the + correct action. + + + 100"; +if ($res = $conn->query($sql)) { + + // Check the number of rows that match the SELECT statement + if ($res->fetchSingle() > 0) { + + // Issue the real SELECT statement and work with the results + $sql = "SELECT name FROM fruit WHERE calories > 100"; + foreach ($conn->query($sql) as $row) { + print "Name: " . $row['NAME'] . "\n"; + } + } + // No rows matched -- do something else + else { + print "No rows matched the query."; + } +} + +$res = null; +$conn = null; +?> +]]> + + &example.outputs; + + + + + + + + + &reftitle.seealso; + + + PDOStatement::query + PDOStatement::fetchSingle + + +