LanceT Posted January 30, 2007 Share Posted January 30, 2007 Lets say I have an script that inserts some stuff to a database and I want to check if there is a duplicate entry, and if so, prevent the user from adding the stuff (in this case, a username). Any way to do this and using the least amount of system resources? The list that the code will be checking will be very long. Quote Link to comment Share on other sites More sharing options...
artacus Posted January 30, 2007 Share Posted January 30, 2007 Well on the mysql side, you'll add a unique index on that field. That will prevent dups from being added. But most times you'll check if the record is there; if it is, update it; if not insert it. Quote Link to comment Share on other sites More sharing options...
LanceT Posted January 30, 2007 Author Share Posted January 30, 2007 Well lets say we want to register and it's a username. Obviously we can't have duplicate usernames. So I want to display an error message if the username is taken.I'm pretty sure this syntax won't work, but something like this.[code]if ( $username exists ) {echo "sorry, that username is taken";}[/code]Some code would be helpful!Thanks guys. Quote Link to comment Share on other sites More sharing options...
fenway Posted January 30, 2007 Share Posted January 30, 2007 Well, like artacus said, if you have a unique index, you can simply attempt to add it, and catch the error. Quote Link to comment Share on other sites More sharing options...
LanceT Posted February 3, 2007 Author Share Posted February 3, 2007 [quote author=fenway link=topic=124651.msg517276#msg517276 date=1170181761]Well, like artacus said, if you have a unique index, you can simply attempt to add it, and catch the error.[/quote]How do i add the unique index? Quote Link to comment Share on other sites More sharing options...
fenway Posted February 5, 2007 Share Posted February 5, 2007 Depends through what interface.. but ADD UNIQUE INDEX indexName ( columnName) will work too. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.