reference/pdo/pdostatement/rowcount.xml
28529d3539b850e870e3aa97570f4db0e53daa03
...
...
@@ -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,37 @@
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 the SQLite driver at all times,
33
+
and with the PostgreSQL driver only when setting the
34
+
<constant>PDO::ATTR_CURSOR</constant> statement attribute to
35
+
<constant>PDO::CURSOR_SCROLL</constant>.
36
+
</para>
37
+
</note>
38
+
</refsect1>
39
+

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

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

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

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

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

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