Pavel K
Pavel K

Reputation: 235

MySQL PDO - check if INSERT IGNORE INTO was inserted

How I can check if query INSERT IGNORE INTO was correctly inserted to database ?

Now I checked it in that way:

$sql = $this->pdo->prepare("INSERT INGORE INTO ...");
if ( $sql->execute() )
{
    $last_id = $this->pdo->lastInsertId();
}

But execute() always return TRUE when $last_id sometimes return 0 so I think my script is not good.

Thanks.

Upvotes: 2

Views: 1679

Answers (1)

Nick
Nick

Reputation: 147266

PDO::rowCount will return the number of rows affected by an INSERT query.

So you can check

if ( $sql->execute() && $sql->rowCount() > 0 ) {
    // do something
}

Upvotes: 1

Related Questions