PDA

View Full Version : [PHP] Checking for username existance?



Trigs
14-02-2009, 08:18 PM
What is the best way to check to see if a username exists?

I use:



$user_check = mysql_query("SELECT username FROM users WHERE username='$r_username'");
$do_user_check = mysql_num_rows($user_check);

if($do_user_check > 0) {
die('Your username is already in use. Please go <a href="register.php">back</a> and fix it.');
}


But it always returns the "Your username is already in use" error.

hamheyelliot
14-02-2009, 08:45 PM
I've absolutely no clue, but if the number on the 3rd line of that code is '0', doesn't that mean that if there are no instances of the username, it will give the error you specified?

But when push comes to shove, I truly have no idea.

Source
14-02-2009, 08:50 PM
$userQ = mysql_query("SELECT * FROM `users` WHERE `username`= '$r_username'");
$userC = mysql_num_rows($userQ);

if( $userC = 1 ) {
die('Your username is already in use. Please go <a href="register.php">back</a> and fix it.');
}

scottish
14-02-2009, 09:02 PM
$find_user = mysql_query("SELECT username FROM users WHERE username='$r_username'");
if(mysql_num_rows($find_user))
{
die('Your username is already in use. Please go <a href="register.php">back</a> and fix it.');
}

Source
14-02-2009, 09:17 PM
Entrance that won't work ...feel free to correct me if i'm wrong... but it would always die on that message because using an if statement like that basically means:

if the mysql_num_rows function works and returns true then do this:

and not

if the username exists in the database then do this.

:) Sorry if I am incorrect

Blinger1
14-02-2009, 09:22 PM
And with yours i would do a double = mark, so it will be


$userQ = mysql_query("SELECT * FROM `users` WHERE `username`= '$r_username'");
$userC = mysql_num_rows($userQ);

if( $userC == 1 ) {
die('Your username is already in use. Please go <a href="register.php">back</a> and fix it.');
}

scottish
14-02-2009, 09:22 PM
mm i've hardly done PHP but i removed the $r_username bit and replaced it with a random one and searched a db i created when it was on it it gave the message and when it didn't showed clear page (obviously as no other code was on it)...

Calgon
14-02-2009, 09:22 PM
$find_user = mysql_query( "SELECT * FROM `players` WHERE `name` = '$bonkers'" );

if( mysql_num_rows( $find_user ) == 1 )
{
echo "Foo' - that username is taken.";
}

Source
14-02-2009, 09:23 PM
Yes blinger I believe that would be correct, my appologies I kind of rushed the response as no-one was really helping.

Oh and calon, thanks for the wonderful block on msn considering how much I helped you. Your welcome.

Blinger1
14-02-2009, 09:24 PM
haha no problems ;)

Trigs
14-02-2009, 10:59 PM
Thanks guys.

Moh
14-02-2009, 11:08 PM
I would do it like this:



$find_user = mysql_query("SELECT `name` FROM `players` WHERE `name` = '$username'");
if(mysql_num_rows($find_user) != 0) {
echo("$username is currenly unavailable");
} else {
echo("$username is available!");
}

Source
14-02-2009, 11:10 PM
Jack I was going todo it like that aswel, but I didn't want to really add extra lines of code. An ideal way todo it would be do it jacks way then do an ajax request when the field is changed, then display underneath is the username is available or not ^_^.

Iszak
14-02-2009, 11:13 PM
Technically by looking at them all, they should all work as you don't need an operator because the if statement checks if it's true automatically because from what I know.


if (mysql_num_rows($query))

is exactly like


if (mysql_num_rows($query) == true)


same goes with !mysql_num_rows() and == false.

Source
14-02-2009, 11:16 PM
Iszak, what I was trying to decipher on my post a page back is if:



if (mysql_num_rows($query))
would work the same as



if (mysql_num_rows($query) == 1 )
because I have always been lead to believe


if (mysql_num_rows($query))mean if the function was completed and returned without error (bad explanation).

Iszak
14-02-2009, 11:19 PM
Well you'd expect it to be returned without error? I mean.. as long as the mysql_query executes correctly then there shouldn't be a problem with mysql_num_rows and yes those first two works exactly the same because 1 is also known as true, and == isn't a strict check so true, 0, 'true', etc are all executed as true. So by doing == 1 it's the same as == true.

Edit: There is one slight difference actually that I've noticed, if you return numerical values (with the exception of 0) in a function and use == true it seems to validate true, but if you use == 1, it will not validate true on numbers only alternative true syntax.

Calgon
15-02-2009, 09:33 AM
Yes blinger I believe that would be correct, my appologies I kind of rushed the response as no-one was really helping.

Oh and calon, thanks for the wonderful block on msn considering how much I helped you. Your welcome.
ugh, I was a bit drunk and doing other things, you disturbing me about PHP was the least on my mind.

Want to hide these adverts? Register an account for free!