2003-03-15 23:01:35 +00:00
|
|
|
<?xml version="1.0" encoding="iso-8859-1"?>
|
2003-08-22 12:08:49 +00:00
|
|
|
<!-- $Revision: 1.8 $ -->
|
2003-03-15 23:01:35 +00:00
|
|
|
<refentry id="function.mysqli-affected-rows">
|
|
|
|
<refnamediv>
|
|
|
|
<refname>mysqli_affected_rows</refname>
|
2003-05-25 18:02:46 +00:00
|
|
|
<refpurpose>Gets the number of affected rows in a previous MySQL operation</refpurpose>
|
2003-03-15 23:01:35 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
<methodsynopsis>
|
|
|
|
<type>mixed</type><methodname>mysqli_affected_rows</methodname>
|
2003-08-22 12:08:49 +00:00
|
|
|
<methodparam><type>object</type><parameter>link</parameter></methodparam>
|
2003-03-15 23:01:35 +00:00
|
|
|
</methodsynopsis>
|
|
|
|
<para>
|
2003-05-25 19:09:28 +00:00
|
|
|
<function>mysqli_affected_rows</function> returns the number of rows affected by the last
|
|
|
|
INSERT, UPDATE, or DELETE query associated with the provided <parameter>link</parameter>
|
|
|
|
parameter. If the last query was invalid, this function will return -1.
|
2003-05-13 23:12:16 +00:00
|
|
|
</para>
|
|
|
|
<note>
|
|
|
|
<para>
|
2003-05-25 19:09:28 +00:00
|
|
|
When deleting the entire contents of a table (i.e. 'DELETE FROM foo'), this function will
|
|
|
|
not return the number of rows that were actually deleted.
|
2003-05-13 23:12:16 +00:00
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
<para>
|
2003-05-25 19:09:28 +00:00
|
|
|
The <function>mysqli_affected_rows</function> function only works with queries which modify
|
|
|
|
a table. In order to return the number of rows from a SELECT query, use the
|
2003-06-11 13:00:48 +00:00
|
|
|
<function>mysqli_num_rows</function> function instead.
|
2003-05-13 23:12:16 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
2003-05-25 19:09:28 +00:00
|
|
|
<example>
|
2003-05-13 23:12:16 +00:00
|
|
|
<title>Delete-Query</title>
|
2003-08-22 12:08:49 +00:00
|
|
|
<para>Procedural style:</para>
|
2003-05-13 23:12:16 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
/* connect to database */
|
2003-08-22 12:08:49 +00:00
|
|
|
$link = mysqli_connect("localhost", "mysql_user", "mysql_password", "mydb") or
|
|
|
|
die("Could not connect: " . mysqli_connect_error());
|
2003-05-17 14:31:04 +00:00
|
|
|
|
2003-05-13 23:12:16 +00:00
|
|
|
/* this should return the correct numbers of deleted records */
|
2003-08-22 12:08:49 +00:00
|
|
|
mysqli_query($link, "DELETE FROM mytable WHERE id < 10");
|
|
|
|
printf ("Records deleted: %2d\n", mysqli_affected_rows($link));
|
2003-05-13 23:12:16 +00:00
|
|
|
|
|
|
|
/* without a where clause in a delete statement, it should return 0 */
|
2003-08-22 12:08:49 +00:00
|
|
|
mysqli_query($link, "DELETE FROM mytable");
|
|
|
|
printf ("Records deleted: %2d\n", mysqli_affected_rows($link));
|
|
|
|
|
|
|
|
/* close connection */
|
|
|
|
mysqli_close($link);
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
<para>Object oriented style:</para>
|
|
|
|
<programlisting>
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
/* connect to database */
|
|
|
|
$mysql = mysqli_connect("localhost", "mysql_user", "mysql_password", "mydb") or
|
|
|
|
die("Could not connect: " . mysqli_connect_error());
|
|
|
|
|
|
|
|
/* this should return the correct numbers of deleted records */
|
|
|
|
$mysql->query("DELETE FROM mytable WHERE id < 10");
|
|
|
|
printf ("Records deleted: %2d\n", $mysql->affected_rows());
|
|
|
|
|
|
|
|
/* without a where clause in a delete statement, it should return 0 */
|
|
|
|
$mysql->query("DELETE FROM mytable");
|
|
|
|
printf ("Records deleted: %2d\n", $mysql->affected_rows());
|
|
|
|
|
|
|
|
/* close connection */
|
|
|
|
$mysql->close();
|
2003-05-13 23:12:16 +00:00
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
<para>
|
2003-08-22 12:08:49 +00:00
|
|
|
The above examples would produce the following output:
|
2003-05-13 23:12:16 +00:00
|
|
|
<screen>
|
|
|
|
<![CDATA[
|
|
|
|
Records deleted: 10
|
2003-08-22 12:08:49 +00:00
|
|
|
Records deleted: 0
|
2003-05-13 23:12:16 +00:00
|
|
|
]]>
|
|
|
|
</screen>
|
|
|
|
</para>
|
|
|
|
</example>
|
|
|
|
<example>
|
|
|
|
<title>Update-Query</title>
|
2003-08-22 12:08:49 +00:00
|
|
|
<para>Procedural style:</para>
|
2003-05-13 23:12:16 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
/* connect to database */
|
2003-08-22 12:08:49 +00:00
|
|
|
$link = mysqli_connect("localhost", "mysql_user", "mysql_password", "mydb") or
|
|
|
|
die("Could not connect: " . mysqli_connect_error());
|
2003-05-17 14:31:04 +00:00
|
|
|
|
2003-05-13 23:12:16 +00:00
|
|
|
/* Update records */
|
2003-08-22 12:08:49 +00:00
|
|
|
mysqli_query($link, "UPDATE mytable SET used=1 WHERE id < 10");
|
|
|
|
printf ("Updated records: %d\n", mysqli_affected_rows($link));
|
|
|
|
|
|
|
|
/* close connection */
|
|
|
|
mysqli_close($link);
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
<para>Object oriented style:</para>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
/* connect to database */
|
|
|
|
$mysql = mysqli_connect("localhost", "mysql_user", "mysql_password", "mydb") or
|
|
|
|
die("Could not connect: " . mysqli_connect_error());
|
|
|
|
|
|
|
|
/* Update records */
|
|
|
|
$mysql->query("UPDATE mytable SET used=1 WHERE id < 10");
|
|
|
|
printf ("Updated records: %d\n", $mysql->affected_rows($link));
|
|
|
|
|
|
|
|
/* close connection */
|
|
|
|
mysql->close($link);
|
2003-05-13 23:12:16 +00:00
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
<para>
|
2003-08-22 12:08:49 +00:00
|
|
|
The above examples would produce the following output:
|
2003-05-13 23:12:16 +00:00
|
|
|
<screen>
|
|
|
|
<![CDATA[
|
|
|
|
Updated Records: 10
|
|
|
|
]]>
|
|
|
|
</screen>
|
|
|
|
</para>
|
|
|
|
</example>
|
2003-03-15 23:01:35 +00:00
|
|
|
</para>
|
2003-08-22 12:08:49 +00:00
|
|
|
<para>
|
|
|
|
See also: <function>mysqli_num_rows</function>,
|
|
|
|
<function>mysqli_info</function>.
|
|
|
|
</para>
|
2003-03-15 23:01:35 +00:00
|
|
|
</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:"../../../../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
|
|
|
|
-->
|