mysql_affected_rows() :- Returns the number of affected rows in previous MySQL operation.

mysql_affected_rows() is very useful function when we need to know how many rows affected our last MySql operation. This function returns the number of affected rows by the last INSERT, UPDATE, REPLACE or DELETE query.

It will return number of rows affected by our last operation & will return -1 if the last query failed. It last query didn’t affect any row of database than it will return 0.

Example :-

<?php
$link = mysql_connect(‘localhost’, ‘milap_patel’, ‘password’);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}
mysql_select_db(‘milap_patel’);

/* this will return the correct numbers of deleted records */
mysql_query(‘DELETE FROM table_name WHERE field_name = “Smile”‘);
printf(“Records deleted: %d\n”, mysql_affected_rows());

?>

Output :-

Records deleted: number of records your query deleted..

 

3 thoughts on “mysql_affected_rows() :- Returns the number of affected rows in previous MySQL operation.

  1. I just couldn’t depart your site before suggesting that I really enjoyed the standard information a person provide for your visitors? Is going to be back often to check up on new posts

    Like

    • Hey Liza,
      Thanks for your feedback.
      mysql_num_rows() is used with SELECT statements while the mysql_affected_rows() is used with UPDATE, DELETE and INSERT statements.
      mysql_num_rows() returns the number of rows returned by a SELECT statement & affected_rows returns the number of rows that were altered by a statement that modified the content of a database (insert,update, delete etc).

      Like

I like to hear from you about this !!

This site uses Akismet to reduce spam. Learn how your comment data is processed.