mysql_affected_rows

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

Version :

(PHP 3, PHP 4, PHP 5)

Syntax :

int mysql_affected_rows ([ resource $link_identifier ] )

Description : mysql_affected_rows() returns the number of rows affected by the last INSERT, UPDATE or DELETE query associated

with link_identifier.

This function returns the number of affected rows on success, or -1 if the last operation failed.

Example : Delete Query

<?php $con = mysql_connect("localhost","user","password"); if (!$con)   {   die("Could not con: " . mysql_error());   } mysql_select_db("mydb"); mysql_query("DELETE FROM mytable WHERE id < 8"); $row = mysql_affected_rows(); echo "Records deleted: " . $row; mysql_close($con); ?>

The output of the code above could be: Records deleted : 7

Example : Update Query

<?php
$con= mysql_connect("localhost","user","password");
if (!$con) {
die("Could not connect:" . mysql_error());
}
mysql_select_db("mydb");
mysql_query("UPDATE mytable SET used=1 WHERE id < 8");

$row=mysql_affected_rows(); echo "Updated records: ".$row;

mysql_close($con);

?>

The output of the code above could be: Updated records : 7
<?
function mysql_affected_rows($con = NULL)
       {
        $con = mysql_resolve_link($con);
        return mysqli_affected_rows($con);
       }
?>

Post Comment
Login to post comments