dextrocity Posted March 5, 2008 Share Posted March 5, 2008 Hey guys total newbie here. How can we check the database for duplicate records before we insert a record? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/94584-check-duplicate-before-insert-query/ Share on other sites More sharing options...
blackwinter Posted March 5, 2008 Share Posted March 5, 2008 Well theres a few ways but to do it simply.... Select COUNT(*) FROM database WHERE username_column = $username_variable Quote Link to comment https://forums.phpfreaks.com/topic/94584-check-duplicate-before-insert-query/#findComment-484315 Share on other sites More sharing options...
sKunKbad Posted March 5, 2008 Share Posted March 5, 2008 declare one field to be a primary key, mysql won't let you insert the same primary key again Quote Link to comment https://forums.phpfreaks.com/topic/94584-check-duplicate-before-insert-query/#findComment-484317 Share on other sites More sharing options...
roopurt18 Posted March 5, 2008 Share Posted March 5, 2008 The best way to do this would be to place a UNIQUE constraint on the proper columns in your table. The database will handle the check for you when you attempt to INSERT or UPDATE and the query will fail. Quote Link to comment https://forums.phpfreaks.com/topic/94584-check-duplicate-before-insert-query/#findComment-484319 Share on other sites More sharing options...
uramagget Posted March 5, 2008 Share Posted March 5, 2008 Well theres a few ways but to do it simply.... Select COUNT(*) FROM database WHERE username_column = $username_variable In addition to that $sql = "SELECT COUNT(*) FROM database WHERE username_column = $username_variable"; $result = mysql_query($sql); if (mysql_num_rows($result) > 0) { //username taken } else { //proceed } Quote Link to comment https://forums.phpfreaks.com/topic/94584-check-duplicate-before-insert-query/#findComment-484326 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.