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

177 lines
5.1 KiB
XML
Raw Normal View History

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.17 $ -->
<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>
&reftitle.returnvalues;
<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>
&reftitle.seealso;
<para>
<function>mysqli_num_rows</function>&listendand;
<function>mysqli_info</function>.
</para>
</refsect1>
<refsect1>
&reftitle.examples;
<example>
<title>Object oriented style</title>
<programlisting role="php">
<![CDATA[
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* 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);
/* delete rows */
$mysqli->query("DELETE FROM Language WHERE Percentage < 50");
printf("Affected rows (DELETE): %d\n", $mysqli->affected_rows);
/* select all rows */
$result = $mysqli->query("SELECT CountryCode FROM Language");
printf("Affected rows (SELECT): %d\n", $mysqli->affected_rows);
$result->close();
/* Delete table Language */
$mysqli->query("DROP TABLE Language");
/* close connection */
$mysqli->close();
?>
]]>
</programlisting>
</example>
<example>
<title>Procedural style</title>
<programlisting role="php">
<![CDATA[
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
if (!$link) {
printf("Can't connect to localhost. Error: %s\n", mysqli_connect_error());
exit();
}
/* Insert rows */
mysqli_query($link, "CREATE TABLE Language SELECT * from CountryLanguage");
printf("Affected rows (INSERT): %d\n", mysqli_affected_rows($link));
mysqli_query($link, "ALTER TABLE Language ADD Status int default 0");
/* 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");
/* close connection */
mysqli_close($link);
?>
]]>
</programlisting>
</example>
&example.outputs;
<screen>
<![CDATA[
Affected rows (INSERT): 984
Affected rows (UPDATE): 168
Affected rows (DELETE): 815
Affected rows (SELECT): 169
]]>
</screen>
</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
-->