Welcome Guest, Not a member yet? Register   Sign In
Affected rows VS Matched rows
#1

[eluser]Alexxz[/eluser]
Quote:$this->db->affected_rows()

Displays the number of affected rows, when doing "write" type queries (insert, update, etc.).
But I have a big problem with it. Because in update query it shows not matched rows, but really affected rows.

Code:
mysql> create table test (a int, b int);
Query OK, 0 rows affected (0.00 sec)

mysql> insert into test values (1, 2);
Query OK, 1 row affected (0.00 sec)

mysql> update test set b = 2 where a = 1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

Some kind of discussion you can find in php.net site in comments.

Is it possible to make function which returns "Rows matched"? May be User Guide needs correction?
What do you think about this?
#2

[eluser]Alexxz[/eluser]
What do you think about that way of implementation from PHP.net web site?

Quote:
Code:
function mysql_modified_rows () {
        $info_str = mysql_info();
        $a_rows = mysql_affected_rows();
        ereg("Rows matched: ([0-9]*)", $info_str, $r_matched);
        return ($a_rows < 1)?($r_matched[1]?$r_matched[1]:0):$a_rows;
        }
#3

[eluser]xwero[/eluser]
Have you checked the mysql driver code to see if there is a hack for this behaviour?
#4

[eluser]Alexxz[/eluser]
[quote author="xwero" date="1196948000"]Have you checked the mysql driver code to see if there is a hack for this behaviour?[/quote]

What kind of hack? there is only
Code:
function affected_rows()
    {
        return @mysql_affected_rows($this->conn_id);
    }
I can't find any related methods.
#5

[eluser]xwero[/eluser]
I've read there are some oddities using the mysql_affected_rows function but i think it's correct most of the time, i never had any problems with it.

If you really want to be sure you get the correct number of affected rows i guess there is no other solution than to get the fields of rows you want to change before you update and after you update an then check the difference.
Or if you want to be sure everything is updated you could do a select-delete-insert chain of events. But both options require much resources.




Theme © iAndrew 2016 - Forum software by © MyBB