php-doc-en/reference/mysqli/functions/mysqli-affected-rows.xml

153 lines
4.9 KiB
XML
Raw Normal View History

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.13 $ -->
<refentry id="function.mysqli-affected-rows">
<refnamediv>
<refname>mysqli_affected_rows</refname>
<refname>mysqli->affected_rows</refname>
<refpurpose>Gets the number of affected rows in a previous MySQL operation</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<para>Procedural style:</para>
<methodsynopsis>
<type>mixed</type><methodname>mysqli_affected_rows</methodname>
<methodparam><type>object</type><parameter>link</parameter></methodparam>
</methodsynopsis>
<para>Object oriented style (property):</para>
<classsynopsis>
<ooclass><classname>mysqli</classname></ooclass>
<fieldsynopsis><type>mixed</type><varname>affected_rows</varname></fieldsynopsis>
</classsynopsis>
<para>
<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.
</para>
<note>
<para>
For SELECT statements <function>mysqli_affected_rows</function> works like
<function>mysqli_num_rows</function>.
</para>
</note>
<para>
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.
</para>
</refsect1>
<refsect1>
<title>Return Values</title>
<para>
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.
</para>
<note>
<para>
If the number of affected rows is greater than maximal int value, the number of affected rows
will be returned as a string.
</para>
</note>
</refsect1>
<refsect1>
<title>See also</title>
<para>
<function>mysqli_num_rows</function>,
<function>mysqli_info</function>.
</para>
</refsect1>
<refsect1>
<title>Example</title>
<para>
<example>
<title>Object oriented style</title>
<programlisting role="php">
<![CDATA[
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* create table and insert some data */
$mysqli->query("DROP TABLE IF EXISTS affected_rows");
$mysqli->query("CREATE TABLE affected_rows (a int)");
$mysqli->query("INSERT INTO affected_rows VALUES (1),(2),(3),(4)");
/* update values and retrieve number of affected rows */
$mysqli->query("UPDATE affected_rows SET a=5 WHERE a=1");
printf("Affected rows (update): %d\n", $mysqli->affected_rows);
/* delete rows and retrieve number of affected_rows */
$mysqli->query("DELETE FROM affected_rows WHERE a < 4");
printf("Affected rows (delete): %d\n", $mysqli->affected_rows);
/* select all rows and retrieve number of affected_rows */
$mysqli->query("SELECT a FROM affected_rows");
printf("Affected rows (select): %d\n", $mysqli->affected_rows);
/* close connection */
$mysqli->close();
?>
]]>
</programlisting>
</example>
<example>
<title>Procedural style</title>
<programlisting role="php">
<![CDATA[
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "test")
or die("Can't connect to MySQL server on localhost");
/* create table and insert some data */
mysqli_query($link, "DROP TABLE IF EXISTS affected_rows");
mysqli_query($link, "CREATE TABLE affected_rows (a int)");
mysqli_query($link, "INSERT INTO affected_rows VALUES (1),(2),(3),(4)");
/* update values and retrieve number of affected rows */
mysqli_query($link, "UPDATE affected_rows SET a=5 WHERE a=1");
printf("Affected rows (update): %d\n", mysqli_affected_rows($link));
/* delete rows and retrieve number of affected_rows */
mysqli_query($link, "DELETE FROM affected_rows WHERE a < 4");
printf("Affected rows (delete): %d\n", mysqli_affected_rows($link));
/* select all rows and retrieve number of affected_rows */
mysqli_query($link, "SELECT a FROM affected_rows");
printf("Affected rows (select): %d\n", mysqli_affected_rows($link));
/* close connection */
mysqli_close($link);
?>
]]>
</programlisting>
</example>
</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:"../../../../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
-->