I have a mysql ON DUPLICATE KEY UPDATE statement that is running successfuly.
Although in the manual it says that for a row inserted the affected rows is 1 and an updated row is 2. I figured I could work out the number of inserted rows by simply doing;
$i = count($insert); // the insert array
// execute the query here
$inserted = ($i * 2) - $q -> rowCount();
The equation that equals $inserted returns the number of inserted rows (according to mysql).
Though on my laptop running wamp, when inserting with only duplicate values I get a row count of 0! Why could this be?
Thanks
The
rowCount()function will only return 1 or 2, denoting an update or an insert. Using theON DUPLICATE KEY UPDATEmeans you forgo the knowing how many rows were affected if you do multiple insert/updates.In your instance, it could be that you are getting a row count of 0 because if you do update with identical values the return value is 0 (i.e. no change). A comment on the PHP manual suggests you can add the PDO attribute
PDO::MYSQL_ATTR_FOUND_ROWSto true to get how many rows your update-query actually found/matched.E.g.