-6

I want avoid to display an error when is inserted a duplicate entry in the database.

I have added this "@" but the error is always displayed:

$result = @mysql_query($query) or die("Query failed insert. Mysql error: ".mysql_error() );

output:

Query failed insert. Mysql error: Duplicate entry for key 'url'

How to avoid to show an error when is inserted a duplicate entry ?

xRobot
  • 25,579
  • 69
  • 184
  • 304
  • 2
    @juergend, considering his rep, I think he just has not had his morning coffee yet. – Mark Tomlin Apr 24 '12 at 12:47
  • 2
    @MarkTomlin oh. don't judge people by their rep. Considering the *code*, he have not a slightest idea what does his code do. – Your Common Sense Apr 24 '12 at 12:50
  • @YourCommonSense, you have a perfect username for this site. But, I like to give people the benefit of the doubt. – Mark Tomlin Apr 24 '12 at 12:51
  • This question isn't particularly well phrased - it took the comments on the answers to actually work out what's _really_ being asked. – CD001 Apr 24 '12 at 13:11
  • @MarkTomlin well, unfortunately, I have way too much experience in both PHP and answering questions, so it leave no place for doubts... :( – Your Common Sense Apr 24 '12 at 13:12
  • @CD001 that's known problem with this site. Most answers just triggered by the keywords in the question (or even just question title). People seldom trying to get the meaning of the question but rather tend to directly answer to some keywords. It is "fastest gun in the west" problem partially to blame for this. And whole Stackoverflow paradigm, which encourage votes, not real help. – Your Common Sense Apr 24 '12 at 13:22

2 Answers2

12

Adding this "@" is a most stupid thing a developer can do ever. You are confusing the error message with the error itself. You have to fix the error, not the message.

To fix this one, you have to use IGNORE keyword in your query

INSERT IGNORE INTO ...

Quite ironically, you actually should remove the die() statement, but only to replace it with way more reliable and safe trigger_error(), so, the code should be

$result = mysql_query($query) or trigger_error(mysql_error()." in ".$query);
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 3
    @xRobot: You go into your house, and see a gigantic crack in the wall, what do you do? You ignore it? No, you call someone to fix it. That's exactly what you should do with errors as well. – Madara's Ghost Apr 24 '12 at 12:52
  • 1
    @xRobot Because error messages are your friends, not foes. The best friends a developer can have. One have to encourage error messages to show up. While to avoid an error, you have to handle it. Like that poor MarkTomlin guy, you are mixing error message with error itself. you have to fix the error, not the message. – Your Common Sense Apr 24 '12 at 12:55
  • Might not be an error - might be a duplicate `UNIQUE` field, such as an email address - there's nothing really wrong with the code but the user input... it might be desirable to just skip the `INSERT` if the record already exists. Although it could also be desirable to do something like `ON DUPLICATE KEY UPDATE` ... the question is a little ambiguous about that. – CD001 Apr 24 '12 at 12:55
  • Ok, but in this case is different. There is a constraint in the my database and the column url is unique so if the url is reinserted then I don't want show an error... simply don't insert it. In the house's example: a gigantic crack can be a work of modern art. – xRobot Apr 24 '12 at 12:57
  • I gave you a solution for this in my answer. – Your Common Sense Apr 24 '12 at 12:59
  • @YourCommonSense - that's why I've upvoted this answer - `IGNORE` is the right thing to do in this circumstance ;) – CD001 Apr 24 '12 at 13:00
5

Well, like other answers, you could remove the die clause afterwards, but that would be a wrong solution to the problem you have in hand!.

You need to fix the problem causing you to have multiple entries, rather than sweep the error under the carpet and hope for the best!

Don't ignore your errors, FIX THEM!

Community
  • 1
  • 1
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • To avoid to check if an URL is already been inserted and then insert it ( SELECT + INSERT ), I thought of make 1 single query and if the URL is already in the database, then do nothing. Is this a wrong approach ? – xRobot Apr 24 '12 at 13:02