PHP mysql_affected_rows() Function Example

PHP mysql_affected_rows() Function Example


Posted in : PHP Posted on : February 5, 2011 at 5:15 PM Comments : [ 0 ]

The mysql_affected_rows() function is used to get number of affected rows in the mysql operation.

PHP mysql_affected_rows() Function Example:

The mysql_affected_rows() function is used to get number of affected rows in the mysql operation. It return number of affected rows on success and -1 if query failed.

Example:

<?php
$connection = mysql_connect("localhost","root","root");

if (!$connection) {
die('PHP Mysql database connection could not connect : ' . mysql_error());
}
else{ 
$db_name = "php_mysql";

mysql_select_db($db_name, $connection);
$sql="delete from user where user_id <= 2;";
mysql_query($sql);
printf("Records deleted: %d\n", mysql_affected_rows()); 
}
mysql_close($connection); 
?> 

The database user table data(records) is:

In this example we have execute delete query for delete records form the user table that have user_id equals or less then 2. The mysql_affected_rows() function return the number of affected rows for this operation.

Output:

After running this example the browser output is:

Records deleted: 2 

And the database user table records is:

Download Example Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics