artvolk
artvolk

Reputation: 9518

PDO do not run same queries twice?

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!

Upvotes: 3

Views: 572

Answers (1)

Quassnoi
Quassnoi

Reputation: 425341

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.

Upvotes: 6

Related Questions