Jump to content


Photo

Checking duplicate user in a DATABASE?


  • Please log in to reply
12 replies to this topic

#1 bilis_money

bilis_money
  • Members
  • PipPipPip
  • Advanced Member
  • 621 posts

Posted 05 August 2006 - 12:38 PM

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



#2 silentwf

silentwf
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 05 August 2006 - 12:45 PM

Hello bilis
Anyways, I ran into this problem myself, but now I can provide you with the answer  :D
$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
}
Hope this helps

#3 ignace

ignace
  • Moderators
  • Now mod flavored
  • 6,431 posts
  • LocationBelgium

Posted 05 August 2006 - 12:51 PM

I normally set this into the database that the username has to be an unique value
Also silentwf I do not recomment using:

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


#4 silentwf

silentwf
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 05 August 2006 - 01:01 PM

How come?

#5 bilis_money

bilis_money
  • Members
  • PipPipPip
  • Advanced Member
  • 621 posts

Posted 05 August 2006 - 01:12 PM

ok, thanks i'll try it.


#6 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 05 August 2006 - 01:13 PM

Forgive me, but why don't you just do a

$query  =  "SELECT COUNT(*) FROM users WHERE username='$username'";

and if the result > 1 you know you have duplicate(s).

RTFM is an almost extinct art form, it should be subsidized.

#7 xec

xec
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 05 August 2006 - 01:18 PM

"i would like to check a user duplicate inside a MySQL database."


Guys , what if i want to get count +  user name that have duplcate entry through a sql  query with out providin any $username



#8 ignace

ignace
  • Moderators
  • Now mod flavored
  • 6,431 posts
  • LocationBelgium

Posted 05 August 2006 - 01:25 PM

xec now your obsetly making it difficult, you need the where clausule otherwise you can not check where something is equal/duplicate

$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));


#9 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 05 August 2006 - 01:26 PM

Try this one:
select count(naam) , naam from xx group by naam;

RTFM is an almost extinct art form, it should be subsidized.

#10 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 05 August 2006 - 01:39 PM

Can I guess the next question: how do I remove the rows with the duplicate usernames?

RTFM is an almost extinct art form, it should be subsidized.

#11 xec

xec
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 05 August 2006 - 01:46 PM

ronverdonk , this query doesnt give us the solution...


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

#12 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 05 August 2006 - 01:56 PM

Last try. If you ONLY want to display the usernames that are duplicate with their occurrence count:
select count(username) as no , username from yourtable group by username having no>1

RTFM is an almost extinct art form, it should be subsidized.

#13 xec

xec
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 05 August 2006 - 02:31 PM

: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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users