dbs Mon Feb 28 19:38:00 2005 EDT
Modified files:
/phpdoc/en/reference/pdo/functions PDOStatement-rowCount.xml
Log:
Add an example for counting rows in SELECT statements.
Probably still need to apply a <blink> tag to this.
http://cvs.php.net/diff.php/phpdoc/en/reference/pdo/functions/PDOStatement-rowCount.xml?r1=1.4&r2=1.5&ty=u
Index: phpdoc/en/reference/pdo/functions/PDOStatement-rowCount.xml
diff -u phpdoc/en/reference/pdo/functions/PDOStatement-rowCount.xml:1.4
phpdoc/en/reference/pdo/functions/PDOStatement-rowCount.xml:1.5
--- phpdoc/en/reference/pdo/functions/PDOStatement-rowCount.xml:1.4 Wed Jan
26 00:35:40 2005
+++ phpdoc/en/reference/pdo/functions/PDOStatement-rowCount.xml Mon Feb 28
19:37:59 2005
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='iso-8859-1'?>
-<!-- $Revision: 1.4 $ -->
+<!-- $Revision: 1.5 $ -->
<!-- Generated by xml_proto.php v2.1. Found in /scripts directory of phpdoc.
-->
<refentry id="function.PDOStatement-rowCount">
<refnamediv>
@@ -8,8 +8,8 @@
Returns the number of rows affected by the last SQL statement
</refpurpose>
</refnamediv>
- <refsect1>
- <title>Description</title>
+ <refsect1 role="description">
+ &reftitle.description;
<methodsynopsis>
<type>int</type><methodname>PDOStatement::rowCount</methodname>
<void/>
@@ -28,8 +28,17 @@
behaviour is not guaranteed for all databases and should not be relied
on for portable applications.
</para>
- <example><title>Return the number of deleted rows</title>
- <programlisting role="php">
+ </refsect1>
+ <refsect1 role="examples">
+ &reftitle.examples;
+ <para>
+ <example>
+ <title>Return the number of deleted rows</title>
+ <para>
+ <function>PDOStatement::rowCount</function> returns the number of
+ rows affected by a DELETE, INSERT, or UPDATE statement.
+ </para>
+ <programlisting role="php">
<![CDATA[
<?php
/* Delete all rows from the FRUIT table */
@@ -42,8 +51,73 @@
print("Deleted $count rows.\n");
?>
]]>
- </programlisting>
- </example>
+ </programlisting>
+ &example.outputs;
+ <screen>
+<![CDATA[
+Deleted 9 rows.
+]]>
+ </screen>
+ </example>
+ <example>
+ <title>Counting rows returned by a SELECT statement</title>
+ <para>
+ For most databases, <function>PDOStatement::rowCount</function> does not
+ return the number of rows affected by a SELECT statement. Instead, use
+ <function>PDO::query</function> to issue a SELECT COUNT(*) statement
+ with the same predicates as your intended SELECT statement, then use
+ <function>PDOStatement::fetchSingle</function> to retrieve the number
+ of rows that will be returned. Your application can then perform the
+ correct action.
+ </para>
+ <programlisting role="php">
+<![CDATA[
+<?php
+$sql = "SELECT COUNT(*) FROM fruit WHERE calories > 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;
+?>
+]]>
+ </programlisting>
+ &example.outputs;
+ <screen>
+<![CDATA[
+apple
+banana
+orange
+pear
+]]>
+ </screen>
+ </example>
+
+ </para>
+ </refsect1>
+ <refsect1 role="seealso">
+ &reftitle.seealso;
+ <para>
+ <simplelist>
+ <member><function>PDOStatement::query</function></member>
+ <member><function>PDOStatement::fetchSingle</function></member>
+ </simplelist>
+ </para>
+
</refsect1>
</refentry>