reference/pdo/pdostatement/rowcount.xml
a5950d8ae47e8befb9fa5f774ddb96a860833ed5
...
...
@@ -1,4 +1,4 @@
1
-
<?xml version="1.0" encoding="UTF-8"?>
1
+
<?xml version="1.0" encoding="utf-8"?>
2
2
<!-- $Revision$ -->
3
3
<refentry xml:id="pdostatement.rowcount" xmlns="http://docbook.org/ns/docbook">
4
4
<refnamediv>
...
...
@@ -9,23 +9,36 @@
9
9
</refnamediv>
10
10
<refsect1 role="description">
11
11
&reftitle.description;
12
-
<methodsynopsis>
13
-
<type>int</type><methodname>PDOStatement::rowCount</methodname>
12
+
<methodsynopsis role="PDOStatement">
13
+
<modifier>public</modifier> <type>int</type><methodname>PDOStatement::rowCount</methodname>
14
14
<void/>
15
15
</methodsynopsis>
16
16

17
17
<para>
18
-
<function>PDOStatement::rowCount</function> returns the number of
18
+
<methodname>PDOStatement::rowCount</methodname> returns the number of
19
19
rows affected by the last DELETE, INSERT, or UPDATE statement
20
20
executed by the corresponding <literal>PDOStatement</literal> object.
21
21
</para>
22
22
<para>
23
-
If the last SQL statement executed by the associated
24
-
<literal>PDOStatement</literal> was a SELECT statement, some databases
25
-
may return the number of rows returned by that statement. However, this
23
+
For statements that produce result sets, such as <literal>SELECT</literal>,
24
+
the behavior is undefined and can be different for each driver.
25
+
Some databases may return the number of rows produced by that statement
26
+
(e.g. MySQL in buffered mode), but this
26
27
behaviour is not guaranteed for all databases and should not be relied
27
28
on for portable applications.
28
29
</para>
30
+
<note>
31
+
<para>
32
+
This method returns "0" (zero) with PostgreSQL driver when setting the
33
+
<constant>PDO::ATTR_CURSOR</constant> statement attribute to
34
+
<constant>PDO::CURSOR_SCROLL</constant>.
35
+
</para>
36
+
</note>
37
+
</refsect1>
38
+

39
+
<refsect1 role="parameters">
40
+
&reftitle.parameters;
41
+
&no.function.parameters;
29
42
</refsect1>
30
43
31
44
<refsect1 role="returnvalues">
...
...
@@ -35,13 +48,18 @@
35
48
</para>
36
49
</refsect1>
37
50

51
+
<refsect1 role="errors">
52
+
&reftitle.errors;
53
+
&pdo.errors;
54
+
</refsect1>
55
+
38
56
<refsect1 role="examples">
39
57
&reftitle.examples;
40
58
<para>
41
59
<example>
42
60
<title>Return the number of deleted rows</title>
43
61
<para>
44
-
<function>PDOStatement::rowCount</function> returns the number of
62
+
<methodname>PDOStatement::rowCount</methodname> returns the number of
45
63
rows affected by a DELETE, INSERT, or UPDATE statement.
46
64
</para>
47
65
<programlisting role="php">
...
...
@@ -52,13 +70,13 @@ $del = $dbh->prepare('DELETE FROM fruit');
52
70
$del->execute();
53
71

54
72
/* Return number of rows that were deleted */
55
-
print("Return number of rows that were deleted:\n");
73
+
print "Return number of rows that were deleted:\n";
56
74
$count = $del->rowCount();
57
-
print("Deleted $count rows.\n");
75
+
print "Deleted $count rows.\n";
58
76
?>
59
77
]]>
60
78
</programlisting>
61
-
&example.outputs;
79
+
&example.outputs.similar;
62
80
<screen>
63
81
<![CDATA[
64
82
Return number of rows that were deleted:
...
...
@@ -69,47 +87,27 @@ Deleted 9 rows.
69
87
<example>
70
88
<title>Counting rows returned by a SELECT statement</title>
71
89
<para>
72
-
For most databases, <function>PDOStatement::rowCount</function> does not
90
+
For most databases, <methodname>PDOStatement::rowCount</methodname> does not
73
91
return the number of rows affected by a SELECT statement. Instead, use
74
-
<function>PDO::query</function> to issue a SELECT COUNT(*) statement
92
+
<methodname>PDO::query</methodname> to issue a SELECT COUNT(*) statement
75
93
with the same predicates as your intended SELECT statement, then use
76
-
<function>PDOStatement::fetchColumn</function> to retrieve the number
77
-
of rows that will be returned. Your application can then perform the
78
-
correct action.
94
+
<methodname>PDOStatement::fetchColumn</methodname> to retrieve the number
95
+
of matching rows.
79
96
</para>
80
97
<programlisting role="php">
81
98
<![CDATA[
82
99
<?php
83
100
$sql = "SELECT COUNT(*) FROM fruit WHERE calories > 100";
84
-
if ($res = $conn->query($sql)) {
101
+
$res = $conn->query($sql);
102
+
$count = $res->fetchColumn();
85
103

86
-
/* Check the number of rows that match the SELECT statement */
87
-
if ($res->fetchColumn() > 0) {
88
-

89
-
/* Issue the real SELECT statement and work with the results */
90
-
$sql = "SELECT name FROM fruit WHERE calories > 100";
91
-
foreach ($conn->query($sql) as $row) {
92
-
print "Name: " . $row['NAME'] . "\n";
93
-
}
94
-
}
95
-
/* No rows matched -- do something else */
96
-
else {
97
-
print "No rows matched the query.";
98
-
}
99
-
}
100
-

101
-
$res = null;
102
-
$conn = null;
103
-
?>
104
+
print "There are " . $count . " matching records.";
104
105
]]>
105
106
</programlisting>
106
-
&example.outputs;
107
+
&example.outputs.similar;
107
108
<screen>
108
109
<![CDATA[
109
-
apple
110
-
banana
111
-
orange
112
-
pear
110
+
There are 2 matching records.
113
111
]]>
114
112
</screen>
115
113
</example>
...
...
@@ -120,15 +118,14 @@ pear
120
118
&reftitle.seealso;
121
119
<para>
122
120
<simplelist>
123
-
<member><function>PDOStatement::columnCount</function></member>
124
-
<member><function>PDOStatement::fetchColumn</function></member>
125
-
<member><function>PDO::query</function></member>
121
+
<member><methodname>PDOStatement::columnCount</methodname></member>
122
+
<member><methodname>PDOStatement::fetchColumn</methodname></member>
123
+
<member><methodname>PDO::query</methodname></member>
126
124
</simplelist>
127
125
</para>
128
126

129
127
</refsect1>
130
128
</refentry>
131
-

132
129
<!-- Keep this comment at the end of the file
133
130
Local variables:
134
131
mode: sgml
135
132