From 98b9b722e8bbcd3bb30e2c8b483a65f633434820 Mon Sep 17 00:00:00 2001 From: Dan Scott Date: Tue, 1 Mar 2005 00:37:59 +0000 Subject: [PATCH] Add an example for counting rows in SELECT statements. Probably still need to apply a tag to this. git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@181049 c90b9560-bf6c-de11-be94-00142212c4b1 --- .../pdo/functions/PDOStatement-rowCount.xml | 88 +++++++++++++++++-- 1 file changed, 81 insertions(+), 7 deletions(-) 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 + + +