Name

mysql_affected_rows

Synopsis

int mysql_affected_rows([cresource connection])

Returns the number of rows affected by the most recent DELETE, INSERT, or UPDATE statement. If the most recent query failed, -1 is returned. There is an exception to this rule: if all rows are deleted from a table, the function returns zero. The function takes an optional connection resource handle as a parameter. If no parameter is passed, the most recently opened connection that is still open is assumed. The function cannot be used with SELECT statements, where mysql_num_rows( ) should be used instead.

The function may report that zero rows were affected. For example, the query:

DELETE FROM customer WHERE cust_id = 3

always executes, but mysql_affected_rows( ) returns zero if there is no matching row. Similarly, if an UPDATE doesn’t change the database, the function returns zero.

Example

<?php
  $query = "INSERT INTO people 
            VALUES(\"selina\", \"" . crypt("sarah", "se") . "\")";
  
  $connection = mysql_connect("localhost", "fred", "shhh");
  mysql_select_db("wedding", $connection);
  
  $result = mysql_query($query, $connection);
  
  echo "Inserted " . mysql_affected_rows($connection) . " row(s)";
  
?>

Get Managing & Using MySQL, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.