1

I am using mysql_query() to execute UPDATE SQL on my table. I want to know whether the query has updated any row or not.

    // $query contains UPDATE SQL
$result = mysql_query($query, $connection);
if(!$result) {
    die("Database connection failed: " . mysql_error());
}

I tried to check value of $result, but it always comes out to be 1 for both cases(0 or more than 0 rows updated).

Can anyone please help.

Thanks.

Jake
  • 15,539
  • 46
  • 120
  • 195

3 Answers3

5

Use mysql_affected_rows(). It returns the number of modified rows in the last query run.

$numRows = mysql_affected_rows($connection).
jli
  • 6,423
  • 2
  • 27
  • 37
0

Try using mysql_affected_rows().

animuson
  • 52,378
  • 28
  • 138
  • 145
Jonathan Rich
  • 1,720
  • 9
  • 11
0

For completeness, you may also find this answer in SQL via the ROW_COUNT() function, although mysql_affected_rows is better for this task.

pilcrow
  • 54,181
  • 12
  • 87
  • 133