2003-03-15 23:01:35 +00:00
|
|
|
<?xml version="1.0" encoding="iso-8859-1"?>
|
2004-10-31 16:05:14 +00:00
|
|
|
<!-- $Revision: 1.17 $ -->
|
2003-03-15 23:01:35 +00:00
|
|
|
<refentry id="function.mysqli-affected-rows">
|
2004-01-28 23:18:42 +00:00
|
|
|
<refnamediv>
|
|
|
|
<refname>mysqli_affected_rows</refname>
|
|
|
|
<refname>mysqli->affected_rows</refname>
|
2004-03-16 15:36:17 +00:00
|
|
|
<refpurpose>Gets the number of affected rows in a previous MySQL operation</refpurpose>
|
2004-01-28 23:18:42 +00:00
|
|
|
</refnamediv>
|
|
|
|
<refsect1>
|
2004-03-16 15:36:17 +00:00
|
|
|
<title>Description</title>
|
|
|
|
<para>Procedural style:</para>
|
2004-01-28 23:18:42 +00:00
|
|
|
<methodsynopsis>
|
2004-03-16 15:36:17 +00:00
|
|
|
<type>mixed</type><methodname>mysqli_affected_rows</methodname>
|
|
|
|
<methodparam><type>object</type><parameter>link</parameter></methodparam>
|
2004-01-28 23:18:42 +00:00
|
|
|
</methodsynopsis>
|
2004-03-16 15:36:17 +00:00
|
|
|
<para>Object oriented style (property):</para>
|
2004-01-28 23:18:42 +00:00
|
|
|
<classsynopsis>
|
|
|
|
<ooclass><classname>mysqli</classname></ooclass>
|
2004-03-16 15:36:17 +00:00
|
|
|
<fieldsynopsis><type>mixed</type><varname>affected_rows</varname></fieldsynopsis>
|
2004-01-28 23:18:42 +00:00
|
|
|
</classsynopsis>
|
|
|
|
<para>
|
2004-03-16 15:36:17 +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.
|
2004-01-28 23:18:42 +00:00
|
|
|
</para>
|
|
|
|
<note>
|
|
|
|
<para>
|
2004-03-16 15:36:17 +00:00
|
|
|
For SELECT statements <function>mysqli_affected_rows</function> works like
|
|
|
|
<function>mysqli_num_rows</function>.
|
2004-01-28 23:18:42 +00:00
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
<para>
|
2004-03-16 15:36:17 +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
|
|
|
|
<function>mysqli_num_rows</function> function instead.
|
2004-01-28 23:18:42 +00:00
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
<refsect1>
|
2004-10-31 16:05:14 +00:00
|
|
|
&reftitle.returnvalues;
|
2004-01-28 23:18:42 +00:00
|
|
|
<para>
|
2004-03-16 15:36:17 +00:00
|
|
|
An integer greater than zero indicates the number of rows affected or retrieved.
|
|
|
|
Zero indicates that no records where updated for an UPDATE statement, no rows matched
|
|
|
|
the WHERE clause in the query or that no query has yet been executed.
|
|
|
|
-1 indicates that the query returned an error.
|
2004-01-28 23:18:42 +00:00
|
|
|
</para>
|
|
|
|
<note>
|
|
|
|
<para>
|
2004-03-16 15:36:17 +00:00
|
|
|
If the number of affected rows is greater than maximal int value, the number of affected rows
|
|
|
|
will be returned as a string.
|
2004-01-28 23:18:42 +00:00
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
</refsect1>
|
2004-02-19 16:27:40 +00:00
|
|
|
<refsect1>
|
2004-10-31 16:05:14 +00:00
|
|
|
&reftitle.seealso;
|
2004-02-19 16:27:40 +00:00
|
|
|
<para>
|
2004-10-31 16:05:14 +00:00
|
|
|
<function>mysqli_num_rows</function>&listendand;
|
2004-02-19 16:27:40 +00:00
|
|
|
<function>mysqli_info</function>.
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
2004-01-28 23:18:42 +00:00
|
|
|
<refsect1>
|
2004-10-31 16:05:14 +00:00
|
|
|
&reftitle.examples;
|
2004-02-25 21:59:16 +00:00
|
|
|
<example>
|
2004-03-16 15:36:17 +00:00
|
|
|
<title>Object oriented style</title>
|
2004-02-25 21:59:16 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
2004-02-19 16:27:40 +00:00
|
|
|
<?php
|
2004-02-25 21:59:16 +00:00
|
|
|
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
|
2003-08-22 12:08:49 +00:00
|
|
|
|
2004-02-25 21:59:16 +00:00
|
|
|
/* check connection */
|
2004-02-19 16:27:40 +00:00
|
|
|
if (mysqli_connect_errno()) {
|
|
|
|
printf("Connect failed: %s\n", mysqli_connect_error());
|
|
|
|
exit();
|
|
|
|
}
|
2003-08-22 12:08:49 +00:00
|
|
|
|
2004-02-25 21:59:16 +00:00
|
|
|
/* Insert rows */
|
|
|
|
$mysqli->query("CREATE TABLE Language SELECT * from CountryLanguage");
|
|
|
|
printf("Affected rows (INSERT): %d\n", $mysqli->affected_rows);
|
|
|
|
|
|
|
|
$mysqli->query("ALTER TABLE Language ADD Status int default 0");
|
|
|
|
|
|
|
|
/* update rows */
|
|
|
|
$mysqli->query("UPDATE Language SET Status=1 WHERE Percentage > 50");
|
|
|
|
printf("Affected rows (UPDATE): %d\n", $mysqli->affected_rows);
|
2003-08-22 12:08:49 +00:00
|
|
|
|
2004-02-25 21:59:16 +00:00
|
|
|
/* delete rows */
|
|
|
|
$mysqli->query("DELETE FROM Language WHERE Percentage < 50");
|
|
|
|
printf("Affected rows (DELETE): %d\n", $mysqli->affected_rows);
|
2003-08-22 12:08:49 +00:00
|
|
|
|
2004-02-25 21:59:16 +00:00
|
|
|
/* select all rows */
|
|
|
|
$result = $mysqli->query("SELECT CountryCode FROM Language");
|
|
|
|
printf("Affected rows (SELECT): %d\n", $mysqli->affected_rows);
|
2003-08-22 12:08:49 +00:00
|
|
|
|
2004-02-25 21:59:16 +00:00
|
|
|
$result->close();
|
|
|
|
|
|
|
|
/* Delete table Language */
|
|
|
|
$mysqli->query("DROP TABLE Language");
|
2004-01-28 23:18:42 +00:00
|
|
|
|
2004-02-19 16:27:40 +00:00
|
|
|
/* close connection */
|
|
|
|
$mysqli->close();
|
|
|
|
?>
|
2004-02-25 21:59:16 +00:00
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
<example>
|
2004-03-16 15:36:17 +00:00
|
|
|
<title>Procedural style</title>
|
2004-02-25 21:59:16 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
2004-02-19 16:27:40 +00:00
|
|
|
<?php
|
2004-02-25 21:59:16 +00:00
|
|
|
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
|
2004-01-28 23:18:42 +00:00
|
|
|
|
2004-02-25 21:59:16 +00:00
|
|
|
if (!$link) {
|
|
|
|
printf("Can't connect to localhost. Error: %s\n", mysqli_connect_error());
|
|
|
|
exit();
|
|
|
|
}
|
2004-01-28 23:18:42 +00:00
|
|
|
|
2004-02-25 21:59:16 +00:00
|
|
|
/* Insert rows */
|
|
|
|
mysqli_query($link, "CREATE TABLE Language SELECT * from CountryLanguage");
|
|
|
|
printf("Affected rows (INSERT): %d\n", mysqli_affected_rows($link));
|
2004-01-28 23:18:42 +00:00
|
|
|
|
2004-02-25 21:59:16 +00:00
|
|
|
mysqli_query($link, "ALTER TABLE Language ADD Status int default 0");
|
2004-01-28 23:18:42 +00:00
|
|
|
|
2004-02-25 21:59:16 +00:00
|
|
|
/* update rows */
|
|
|
|
mysqli_query($link, "UPDATE Language SET Status=1 WHERE Percentage > 50");
|
|
|
|
printf("Affected rows (UPDATE): %d\n", mysqli_affected_rows($link));
|
|
|
|
|
|
|
|
/* delete rows */
|
|
|
|
mysqli_query($link, "DELETE FROM Language WHERE Percentage < 50");
|
|
|
|
printf("Affected rows (DELETE): %d\n", mysqli_affected_rows($link));
|
|
|
|
|
|
|
|
/* select all rows */
|
|
|
|
$result = mysqli_query($link, "SELECT CountryCode FROM Language");
|
|
|
|
printf("Affected rows (SELECT): %d\n", mysqli_affected_rows($link));
|
|
|
|
|
|
|
|
mysqli_free_result($result);
|
|
|
|
|
|
|
|
/* Delete table Language */
|
|
|
|
mysqli_query($link, "DROP TABLE Language");
|
2004-01-28 23:18:42 +00:00
|
|
|
|
2004-02-19 16:27:40 +00:00
|
|
|
/* close connection */
|
|
|
|
mysqli_close($link);
|
|
|
|
?>
|
2004-02-25 21:59:16 +00:00
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
2004-10-31 16:05:14 +00:00
|
|
|
&example.outputs;
|
2004-02-25 21:59:16 +00:00
|
|
|
<screen>
|
|
|
|
<![CDATA[
|
|
|
|
Affected rows (INSERT): 984
|
|
|
|
Affected rows (UPDATE): 168
|
|
|
|
Affected rows (DELETE): 815
|
|
|
|
Affected rows (SELECT): 169
|
|
|
|
]]>
|
|
|
|
</screen>
|
2004-01-28 23:18:42 +00:00
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
2003-03-15 23:01:35 +00:00
|
|
|
|
|
|
|
<!-- 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
|
|
|
|
-->
|