NYCPHP Meetup

NYPHP.org

[nycphp-talk] mysql_insert_id Strangeness

Analysis & Solutions danielc at analysisandsolutions.com
Tue Aug 5 17:08:44 EDT 2003


Hey Folks:

On Tue, Aug 05, 2003 at 11:20:26AM -0400, Carlos A Hoyos wrote:
> 
> It's safer to check the return value of mysql_query in this case (will
> return false if insertion fails).

Yep.  When mysql_query() fails, I check the value of mysql_errno().  Error 
numbers 1022 and 1062 have to do with duplicate keys on (if I recall 
correctly) primary keys and duplicate keys (or if I recall incorrectly, 
insert vs update queries).

switch ( @mysql_errno($Connection) ) {
    case 1022:
    case 1062:
        # Couldn't insert/update record due to duplicate key.
        break;

    default:
        # Some other database error.  Trap it.
        echo @mysql_error($this->SQLConnection);
        exit;
}

... snipping out old, irrelevant parts... the friendly way to post...

--Dan

-- 
     FREE scripts that make web and database programming easier
           http://www.analysisandsolutions.com/software/
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
 4015 7th Ave #4AJ, Brooklyn NY    v: 718-854-0335   f: 718-854-0409



More information about the talk mailing list