Removed misleading example from rowCount()

The example given was confusing and misleading. If the purpose of it is
to show how to get the count of matching records from the database then
the example can be much shorter. `query()` should not be checked with
`if` statement and there should not be 2 separate queries to the
database if the actual count is not needed. If the number of fetched
records is needed then you can simply use PHP's `count()` function.


git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@351083 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Christoph Michael Becker 2020-10-30 13:13:47 +00:00
parent 11952f8447
commit 03cc650911

View file

@ -66,7 +66,7 @@ print("Deleted $count rows.\n");
?>
]]>
</programlisting>
&example.outputs;
&example.outputs.similar;
<screen>
<![CDATA[
Return number of rows that were deleted:
@ -82,43 +82,22 @@ Deleted 9 rows.
<function>PDO::query</function> to issue a SELECT COUNT(*) statement
with the same predicates as your intended SELECT statement, then use
<function>PDOStatement::fetchColumn</function> to retrieve the number
of rows that will be returned. Your application can then perform the
correct action.
of matching rows.
</para>
<programlisting role="php">
<![CDATA[
<?php
$sql = "SELECT COUNT(*) FROM fruit WHERE calories > 100";
if ($res = $conn->query($sql)) {
$res = $conn->query($sql);
$count = $res->fetchColumn();
/* Check the number of rows that match the SELECT statement */
if ($res->fetchColumn() > 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;
?>
print "There are " . $count . " matching records.";
]]>
</programlisting>
&example.outputs;
&example.outputs.similar;
<screen>
<![CDATA[
apple
banana
orange
pear
There are 2 matching records.
]]>
</screen>
</example>