How to get affected rows after update statement in mysql?

How to get affected rows (and not just the number of rows affected) after the update statement in mysql.

+3
source share
4 answers

mysql_info - get information about the most recent query

$recent = mysql_info();

Useful implementation Example

+2
source

If you want the actual rows, rather than the number of rows affected , just extract them before performing the update.

Then you can compare the update values ​​with the selected values ​​and filter them by difference.

CodeIgniter example:

$arr_where = array('date >=' => date('Y-m-d H:i:s'));

$query = $this->db->get_where('table', $arr_where);

$arr_update = array('status' => 'TRUE');

if ($this->db->update('table', $arr_update, $arr_where)) {

    foreach($query->result() as $row)
        foreach(array_keys($arr_update) as $h)
            if ($row->$h != $arr_update[$h])
                echo "This row ({$row->id}) had it {$h} changed!<br />";

}

Sorry for delivering the solution in CodeIgniter, but I found this to be the easiest example.

, $this->db, . .

+1

You can try:

SET @uids := '';
UPDATE <table>
 SET <column1> = <value>
 WHERE <column2> = <value> 
   AND ( SELECT @uids := CONCAT_WS(',', @uids, id) );
SELECT TRIM(LEADING ',' FROM @uids);
+1
source

Since you are using php, you can use mysql_affected_rows () to get the number of rows affected.

0
source

All Articles