Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if PDO MySQL INSERT was successful with IGNORE

Tags:

mysql

insert

pdo

I understand that I can simply if ($PDO->execute()) { //true } to see if a PDO INSERT was successful, but what if IGNORE is involved?

Eg.

INSERT IGNORE INTO MyTable SET DateTime = CAST(:dateTime AS DATETIME)

If the the record already exists, and so is ignored, will ->execute() return true or false?

I can't seem to find any documentation which tells me.

like image 835
Chuck Le Butt Avatar asked Dec 05 '25 13:12

Chuck Le Butt


2 Answers

Try:

$stmt->execute();
$stmt->rowCount();

That will tell you how many rows were affected by the last query.

like image 175
Ian Wood Avatar answered Dec 07 '25 01:12

Ian Wood


I thought this would garner me a quick answer, but it didn't, so I tested it myself:

If no records are altered as a result of the IGNORE statement, then ->execute() still returns true. It will only return false if there's an error.

like image 35
Chuck Le Butt Avatar answered Dec 07 '25 03:12

Chuck Le Butt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!