Alex Rivera | Logout

PHP/mysql get number of affected rows of UPDATE statement

Asked 2011-12-02T13:14:29.500
11

With php/mysql how can i get the number of rows that a query affected?

what i tried so far:

$result = mysql_query($q);
mysql_num_rows($result);

but it says that Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource

Edit
Report

2 Answers

5

You also may want to use a ROW_COUNT() function, e.g. -

UPDATE table1 SET column1 = 100 WHERE column2 = 10;
SELECT ROW_COUNT();

From the reference - ROW_COUNT() returns the number of rows changed, deleted, or inserted by the last statement if it was an UPDATE, DELETE, or INSERT...

answered 2011-12-02T13:34:57.737
3

clamp,

you need to supply a resource to mysql_affected_rows, not a result record. See the links that the others have posted for additional information.

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
$result = mysql_query($q);
echo mysql_affected_rows($link);
answered 2011-12-02T13:19:53.420

Your Answer