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..

 

Advertisements

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

    • 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 !!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s