PHP to return custom error messages from MySQL

This is a discussion on PHP to return custom error messages from MySQL within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi, I have a database which has two fields which must contain unique date. At the moment I am currently ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-01-2005
Iain
 
Posts: n/a
Default PHP to return custom error messages from MySQL

Hi,

I have a database which has two fields which must contain unique date.

At the moment I am currently using mysql_error() to return the error
message. And this returns "Duplicate entry 'aaa' for key 2" and
Duplicate entry 'bbb' for key 3". I know what each key means but how
can I send a custom error message say field x is already registered on
our system please try again?


Many thanks in advanced.
Reply With Quote
  #2 (permalink)  
Old 02-01-2005
Oli Filth
 
Posts: n/a
Default Re: PHP to return custom error messages from MySQL

Iain wrote:
> Hi,
>
> I have a database which has two fields which must contain unique date.
>
> At the moment I am currently using mysql_error() to return the error
> message. And this returns "Duplicate entry 'aaa' for key 2" and
> Duplicate entry 'bbb' for key 3". I know what each key means but how
> can I send a custom error message say field x is already registered on
> our system please try again?
>
>
> Many thanks in advanced.


Rather than just error-handling, you could add the IGNORE keyword to
your INSERT query, i.e.

INSERT IGNORE INTO table (aaa, bbb) VALUES ($valA, $valB)

Then look at mysql_affected_rows(). If it returns zero, the row was not
added, so you could then do something like:

SELECT COUNT(*) FROM table WHERE aaa=$valA

If COUNT(*) is non-zero, it was aaa that was the duplicate key,
otherwise it was bbb.

Not particularly elegant, but IMO more elegant than causing an error and
then substring matching on the error string (the only real alternative I
can see at the moment).

--
Oli
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 07:38 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0