bilis_money Posted August 5, 2006 Share Posted August 5, 2006 hi,i would like to check a user duplicaet inside a MySQL database.So what would be the right query for this in MySQL or PHP?can you show me example snippet codes?thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/16634-checking-duplicate-user-in-a-database/ Share on other sites More sharing options...
silentwf Posted August 5, 2006 Share Posted August 5, 2006 Hello bilisAnyways, I ran into this problem myself, but now I can provide you with the answer :D[code]$query = "SELECT username FROM users WHERE username='$username'";$result = mysql_query($query);$checkUsername = mysql_fetch_array($result);if!isset($checkUsername['username']){ //if the username is not taken //do something} else { //report error}[/code]Hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/16634-checking-duplicate-user-in-a-database/#findComment-69764 Share on other sites More sharing options...
ignace Posted August 5, 2006 Share Posted August 5, 2006 I normally set this into the database that the username has to be an unique valueAlso silentwf I do not recomment using:[code]<?phpif !isset($checkUsername[...// Instead useif (!in_array($username, $checkUsername)) { ...?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16634-checking-duplicate-user-in-a-database/#findComment-69766 Share on other sites More sharing options...
silentwf Posted August 5, 2006 Share Posted August 5, 2006 How come? Quote Link to comment https://forums.phpfreaks.com/topic/16634-checking-duplicate-user-in-a-database/#findComment-69770 Share on other sites More sharing options...
bilis_money Posted August 5, 2006 Author Share Posted August 5, 2006 ok, thanks i'll try it. Quote Link to comment https://forums.phpfreaks.com/topic/16634-checking-duplicate-user-in-a-database/#findComment-69775 Share on other sites More sharing options...
ronverdonk Posted August 5, 2006 Share Posted August 5, 2006 Forgive me, but why don't you just do a[code]$query = "SELECT COUNT(*) FROM users WHERE username='$username'";[/code]and if the result > 1 you know you have duplicate(s). Quote Link to comment https://forums.phpfreaks.com/topic/16634-checking-duplicate-user-in-a-database/#findComment-69777 Share on other sites More sharing options...
xec Posted August 5, 2006 Share Posted August 5, 2006 "[font=Verdana][u][b]i would like to check a user duplicate inside a MySQL database.[/b][/u][/font]"Guys , what if i want to get count + user name that have duplcate entry through a sql query with out providin any $username Quote Link to comment https://forums.phpfreaks.com/topic/16634-checking-duplicate-user-in-a-database/#findComment-69780 Share on other sites More sharing options...
ignace Posted August 5, 2006 Share Posted August 5, 2006 xec now your obsetly making it difficult, you need the where clausule otherwise you can not check where something is equal/duplicate[code]$query = "SELECT `username`, COUNT(`username`) AS `DuplicateEntries` FROM `users` WHERE `username`='$username'";// will possibly not work... therefore rather use:$Query = mysql_query("SELECT `username` FROM `users` WHERE `username`='$username'");$Rows = count(mysql_fetch_array($Query));[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16634-checking-duplicate-user-in-a-database/#findComment-69783 Share on other sites More sharing options...
ronverdonk Posted August 5, 2006 Share Posted August 5, 2006 Try this one:[code] select count(naam) , naam from xx group by naam;[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16634-checking-duplicate-user-in-a-database/#findComment-69785 Share on other sites More sharing options...
ronverdonk Posted August 5, 2006 Share Posted August 5, 2006 Can I guess the next question: how do I remove the rows with the duplicate usernames? Quote Link to comment https://forums.phpfreaks.com/topic/16634-checking-duplicate-user-in-a-database/#findComment-69787 Share on other sites More sharing options...
xec Posted August 5, 2006 Share Posted August 5, 2006 ronverdonk , this query doesnt give us the solution...i still havent got the answers , its possible . then i have to find out this query.... ;) Quote Link to comment https://forums.phpfreaks.com/topic/16634-checking-duplicate-user-in-a-database/#findComment-69791 Share on other sites More sharing options...
ronverdonk Posted August 5, 2006 Share Posted August 5, 2006 Last try. If you ONLY want to display the usernames that are duplicate with their occurrence count:[code]select count(username) as no , username from yourtable group by username having no>1[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16634-checking-duplicate-user-in-a-database/#findComment-69794 Share on other sites More sharing options...
xec Posted August 5, 2006 Share Posted August 5, 2006 :D great, this works, listing all the names of writer with the no. of time they have been entred....select count(writer_name) as no , writer_name from writer_info group by writer_name Quote Link to comment https://forums.phpfreaks.com/topic/16634-checking-duplicate-user-in-a-database/#findComment-69803 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.