Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top