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. 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 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 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. 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 } 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
Archived
This topic is now archived and is closed to further replies.