tags:

views:

112

answers:

1

Good day!

I'm trying to run the same update statement with the same params twice and it seems that it is not executed in the second case:

$update_query = $this->db->connection->prepare('UPDATE `Table SET `field` = :price WHERE (`partnum` = :partnum)');

$update_query->execute(array('price' => 123, 'partnum' => test));
var_dump($update_query->rowCount()); //returns 1

// If I insert here any statement it works as expected

$update_query->execute(array('price' => 123, 'partnum' => test));
var_dump($update_query->rowCount()); //returns 0!

I do not have mysql query cache enabled.

Thanks!

+4  A: 

If UPDATE changes no data in a row, MySQL doesn't count this row as affected:

mysql> SELECT val FROM t_source2 WHERE id = 1;
+-----+
| val |
+-----+
|  10 |
+-----+
1 row in set (0.00 sec)

mysql> UPDATE t_source2 SET val = 1 WHERE id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> UPDATE t_source2 SET val = 1 WHERE id = 1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

The second UPDATE statement did execute but affected no rows from MySQL's point of view, since it changed nothing.

Quassnoi
The fact that the statement is being executed twice suggests that the OP didn't intend for the first SQL statement to update all the rows.If that's the case, it's worth remembering you can use the `LIMIT` clause on `UPDATE` statements as well as with the more common use for `SELECT`.
drewm