-
Notifications
You must be signed in to change notification settings - Fork 785
/
Copy pathmysql-affected-rows.xml
189 lines (178 loc) · 5.18 KB
/
mysql-affected-rows.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="function.mysql-affected-rows" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>mysql_affected_rows</refname>
<refpurpose>Get number of affected rows in previous MySQL operation</refpurpose>
</refnamediv>
<refsynopsisdiv>
<warning>
&mysql.alternative.note;
<simplelist role="alternatives">
<member><function>mysqli_affected_rows</function></member>
<member><methodname>PDOStatement::rowCount</methodname></member>
</simplelist>
</warning>
</refsynopsisdiv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>int</type><methodname>mysql_affected_rows</methodname>
<methodparam choice="opt"><type>resource</type><parameter>link_identifier</parameter><initializer>NULL</initializer></methodparam>
</methodsynopsis>
<para>
Get the number of affected rows by the last INSERT, UPDATE, REPLACE
or DELETE query associated with <parameter>link_identifier</parameter>.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
&mysql.linkid.description;
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns the number of affected rows on success, and -1 if the last query
failed.
</para>
<para>
If the last query was a DELETE query with no WHERE clause, all
of the records will have been deleted from the table but this
function will return zero with MySQL versions prior to 4.1.2.
</para>
<para>
When using UPDATE, MySQL will not update columns where the new value is the
same as the old value. This creates the possibility that
<function>mysql_affected_rows</function> may not actually equal the number
of rows matched, only the number of rows that were literally affected by
the query.
</para>
<para>
The REPLACE statement first deletes the record with the same primary key
and then inserts the new record. This function returns the number of
deleted records plus the number of inserted records.
</para>
<para>
In the case of "INSERT ... ON DUPLICATE KEY UPDATE" queries, the
return value will be <literal>1</literal> if an insert was performed,
or <literal>2</literal> for an update of an existing row.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>mysql_affected_rows</function> example</title>
<programlisting role="php">
<![CDATA[
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydb');
/* this should return the correct numbers of deleted records */
mysql_query('DELETE FROM mytable WHERE id < 10');
printf("Records deleted: %d\n", mysql_affected_rows());
/* with a where clause that is never true, it should return 0 */
mysql_query('DELETE FROM mytable WHERE 0');
printf("Records deleted: %d\n", mysql_affected_rows());
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
Records deleted: 10
Records deleted: 0
]]>
</screen>
</example>
</para>
<para>
<example>
<title><function>mysql_affected_rows</function> example using transactions</title>
<programlisting role="php">
<![CDATA[
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydb');
/* Update records */
mysql_query("UPDATE mytable SET used=1 WHERE id < 10");
printf ("Updated records: %d\n", mysql_affected_rows());
mysql_query("COMMIT");
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
Updated Records: 10
]]>
</screen>
</example>
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<note>
<title>Transactions</title>
<para>
If you are using transactions, you need to call
<function>mysql_affected_rows</function> after your INSERT, UPDATE, or
DELETE query, not after the COMMIT.
</para>
</note>
<note>
<title>SELECT Statements</title>
<para>
To retrieve the number of rows returned by a SELECT, it is possible to
use <function>mysql_num_rows</function>.
</para>
</note>
<note>
<title>Cascaded Foreign Keys</title>
<para>
<function>mysql_affected_rows</function> does not count rows affected
implicitly through the use of ON DELETE CASCADE and/or ON UPDATE CASCADE
in foreign key constraints.
</para>
</note>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>mysql_num_rows</function></member>
<member><function>mysql_info</function></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->