<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >mysql_affected_rows</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Manual PHP" HREF="index.html"><LINK REL="UP" TITLE="MySQL Functions" HREF="ref.mysql.html"><LINK REL="PREVIOUS" TITLE="MySQL Functions" HREF="ref.mysql.html"><LINK REL="NEXT" TITLE="mysql_change_user" HREF="function.mysql-change-user.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-2"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Manual PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="ref.mysql.html" ACCESSKEY="P" >Înapoi</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.mysql-change-user.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.mysql-affected-rows" ></A >mysql_affected_rows</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN66533" ></A ><P > (PHP 3, PHP 4 , PHP 5)</P >mysql_affected_rows -- Get number of affected rows in previous MySQL operation</DIV ><DIV CLASS="refsect1" ><A NAME="AEN66536" ></A ><H2 >Description</H2 >int <B CLASS="methodname" >mysql_affected_rows</B > ( [resource link_identifier])<BR ></BR ><P > <B CLASS="function" >mysql_affected_rows()</B > returns the number of rows affected by the last INSERT, UPDATE or DELETE query associated with <VAR CLASS="parameter" >link_identifier</VAR >. If the link identifier isn't specified, the last link opened by <A HREF="function.mysql-connect.html" ><B CLASS="function" >mysql_connect()</B ></A > is assumed. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Notã: </B > If you are using transactions, you need to call <B CLASS="function" >mysql_affected_rows()</B > after your INSERT, UPDATE, or DELETE query, not after the commit. </P ></BLOCKQUOTE ></DIV ><P > 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. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Notã: </B > When using UPDATE, MySQL will not update columns where the new value is the same as the old value. This creates the possibility that <B CLASS="function" >mysql_affected_rows()</B > may not actually equal the number of rows matched, only the number of rows that were literally affected by the query. </P ></BLOCKQUOTE ></DIV ><P > <B CLASS="function" >mysql_affected_rows()</B > does not work with SELECT statements; only on statements which modify records. To retrieve the number of rows returned by a SELECT, use <A HREF="function.mysql-num-rows.html" ><B CLASS="function" >mysql_num_rows()</B ></A >. </P ><P > If the last query failed, this function will return -1. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN66559" ></A ><P ><B >Exemplu 1. Delete-Query</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">/* connect to database */<br /></font><font color="#0000BB">$link </font><font color="#007700">= </font><font color="#0000BB">mysql_connect</font><font color="#007700">(</font><font color="#DD0000">'localhost'</font><font color="#007700">, </font><font color="#DD0000">'mysql_user'</font><font color="#007700">, </font><font color="#DD0000">'mysql_password'</font><font color="#007700">);<br />if (!</font><font color="#0000BB">$link</font><font color="#007700">) {<br /> die(</font><font color="#DD0000">'Could not connect: ' </font><font color="#007700">. </font><font color="#0000BB">mysql_error</font><font color="#007700">());<br />}<br /></font><font color="#0000BB">mysql_select_db</font><font color="#007700">(</font><font color="#DD0000">'mydb'</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* this should return the correct numbers of deleted records */<br /></font><font color="#0000BB">mysql_query</font><font color="#007700">(</font><font color="#DD0000">'DELETE FROM mytable WHERE id < 10'</font><font color="#007700">);<br /></font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"Records deleted: %d\n"</font><font color="#007700">, </font><font color="#0000BB">mysql_affected_rows</font><font color="#007700">());<br /><br /></font><font color="#FF8000">/* with a where clause that is never true, it should return 0 */<br /></font><font color="#0000BB">mysql_query</font><font color="#007700">(</font><font color="#DD0000">'DELETE FROM mytable WHERE 0'</font><font color="#007700">);<br /></font><font color="#0000BB">printf</font><font color="#007700">(</font><font color="#DD0000">"Records deleted: %d\n"</font><font color="#007700">, </font><font color="#0000BB">mysql_affected_rows</font><font color="#007700">());<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P > The above example would produce the following output: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Records deleted: 10 Records deleted: 0</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN66565" ></A ><P ><B >Exemplu 2. Update-Query</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">/* connect to database */<br /></font><font color="#0000BB">mysql_connect</font><font color="#007700">(</font><font color="#DD0000">"localhost"</font><font color="#007700">, </font><font color="#DD0000">"mysql_user"</font><font color="#007700">, </font><font color="#DD0000">"mysql_password"</font><font color="#007700">) or<br /> die(</font><font color="#DD0000">"Could not connect: " </font><font color="#007700">. </font><font color="#0000BB">mysql_error</font><font color="#007700">());<br /></font><font color="#0000BB">mysql_select_db</font><font color="#007700">(</font><font color="#DD0000">"mydb"</font><font color="#007700">);<br /><br /></font><font color="#FF8000">/* Update records */<br /></font><font color="#0000BB">mysql_query</font><font color="#007700">(</font><font color="#DD0000">"UPDATE mytable SET used=1 WHERE id < 10"</font><font color="#007700">);<br /></font><font color="#0000BB">printf </font><font color="#007700">(</font><font color="#DD0000">"Updated records: %d\n"</font><font color="#007700">, </font><font color="#0000BB">mysql_affected_rows</font><font color="#007700">());<br /></font><font color="#0000BB">mysql_query</font><font color="#007700">(</font><font color="#DD0000">"COMMIT"</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P > The above example would produce the following output: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Updated Records: 10</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > See also <A HREF="function.mysql-num-rows.html" ><B CLASS="function" >mysql_num_rows()</B ></A >, and <A HREF="function.mysql-info.html" ><B CLASS="function" >mysql_info()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="ref.mysql.html" ACCESSKEY="P" >Înapoi</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Acasã</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.mysql-change-user.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >MySQL Functions</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.mysql.html" ACCESSKEY="U" >Sus</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >mysql_change_user</TD ></TR ></TABLE ></DIV ></BODY ></HTML >