Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Catching MySQL Errors

Status
Not open for further replies.

jamiecottonuk

Technical User
Oct 29, 2007
12
GB
Hi all,

I want to catch a mysql error, how would I go about doing this?

This is my situation. I have set a field in the db to be unique and therefore if the value from the user form matches one in the db then I want to be able to catch the error and do nothing else if the value from the form does not match a value in the db then insert it into the db

I know this is possible in PHP by doing mysql_errno()
Cheers
 
I'm not familiar with MySQL or its specific syntax (at least in how it may differ with SQL Server), but this should be doable in at least two ways (I think). You can send a query to the db to verify if the value exists in the db and, if so, then skip past the insert and present an error message to the user on the front end. Alternatively, you can do it completely in the db by writing a query that will do the same thing (IF EXISTS, then nothing else INSERT). You would probably be more familiar with the syntax than I but that would probably be my preferred solution if given a choice.

A short example of what I'm talking about in my first example would be like this:
Code:
[COLOR=green]'This is ASP file[/color]
strSQL = "SELECT ID_nbr FROM myTable WHERE ID_nbr"
set rs = conn, strSQL
if not rs is nothing then
  [COLOR=green]'From here you can then process the rest of your code and do your INSERT[/color]
else
  [COLOR=green]'if you return nothing, then you can do your error code.[/color]
end if

------------------------------------------------------------------------------------------------------------------------
"Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."
- Winston Churchill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top