MySQL Forums
Forum List  »  Perl

Re: When UPDATE doesn't work..
Posted by: Mike Kruckenberg
Date: September 18, 2006 09:41AM

In MySQL there is a function row_count() that will return the number of affected rows on the previous statement, including 0 if nothing was changed:

mysql> select * from person;
+------+-------+
| id | name |
+------+-------+
| 1 | john |
| 2 | frank |
| 3 | harry |
+------+-------+
3 rows in set (0.00 sec)

mysql> update person set name = 'richard' where id = 3;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select row_count();
+-------------+
| row_count() |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)

mysql> update person set name = 'frank' where id = 4;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0

mysql> select row_count();
+-------------+
| row_count() |
+-------------+
| 0 |
+-------------+
1 row in set (0.00 sec)

You can get at that in the DBI using the rows method:

my $sth = $dbh->do("some sql");
my $count = $sth->rows;

Hope that helps.

Mike Kruckenberg
Co-author of Pro MySQL (Apress)
http://mike.kruckenberg.com

Options: ReplyQuote


Subject
Written By
Posted
September 15, 2006 03:46PM
Re: When UPDATE doesn't work..
September 18, 2006 09:41AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.