Genadinik
Genadinik

Reputation: 18649

Php and Mysql - detecting duplicate entry

I have a MySql query, and I am trying to check if there was a duplicate entry in my PHP code, and I am trying to do something like this:

if(!$result)
{
    $error_message = mysql_error();

        $pos = strpos ( $error_message ,  'Duplicate entry' );

        if($pos === false)
        {
           ... Do some error reporting...

but the line $error_message = mysql_error(); seems to be breaking it. Would someone know why that happens and how to detect a duplicate error message correctly since this is a bit of a hack on my part?

Upvotes: 3

Views: 619

Answers (2)

Chris Henry
Chris Henry

Reputation: 12010

You should look at the behaviour of MySQL's INSERT IGNORE It will allow you to tell MySQL that you're doing an insert where there might be a duplicate, and if so, don't do anything.

Upvotes: 2

lix
lix

Reputation: 517

You could use mysql_errno, or mysqli_errno, and look for the error number 1062, which is the error number for duplicate entries.

Upvotes: 3

Related Questions