user2426701
user2426701

Reputation:

PDO rowCount() alternative

I am using PDO rowCount() to understand whenever an UPDATE was successfull or not. The problem is that rowCount() does not return anything in the case that the query was successfull but there was no row to update.

This is not good for me because in this case I run an INSERT and the mysql returns an error for duplicated keys.

What's the best way in PDO to know if a query was successfull, in this specific case, means when an UPDATE was successfull even though no rows were affected?

Thanks

Upvotes: 1

Views: 582

Answers (2)

Your Common Sense
Your Common Sense

Reputation: 157888

This is a perfect case of "XY problem". But luckily you managed to explain your real problem along with main question. You don't actually need no rowcount() here.

What you really need is a mysql specific INSERT .. ON DUPLICATE KEY UPDATE query

Which will either insert or update the record based on whether it exists or not.

Upvotes: 2

feeela
feeela

Reputation: 29932

Use $stmt->execute() === true to test if a query was successful.

Use $stmt->rowCount() > 0 to see if there where any updates.

For example:

if( $stmt->execute() === true && $stmt->rowCount() > 0 )

Upvotes: -1

Related Questions