Jump to content

Archived

This topic is now archived and is closed to further replies.

bilis_money

Checking duplicate user in a DATABASE?

Recommended Posts

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

Share this post


Link to post
Share on other sites
Hello bilis
Anyways, 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

Share this post


Link to post
Share on other sites
I normally set this into the database that the username has to be an unique value
Also silentwf I do not recomment using:

[code]
<?php
if !isset($checkUsername[...
// Instead use
if (!in_array($username, $checkUsername)) { ...
?>
[/code]

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
"[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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Try this one:
[code] select count(naam) , naam from xx group by naam;[/code]

Share this post


Link to post
Share on other sites
Can I guess the next question: how do I remove the rows with the duplicate usernames?

Share this post


Link to post
Share on other sites
ronverdonk , this query doesnt give us the solution...


i still havent got the answers , its possible . then i have to find out this query....  ;)

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
: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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.