To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Coding

Coding Help with PHP coding

Reply
 
Thread Tools Rate Thread Display Modes
Old 12-06-2003, 08:28 AM   #1
djdaz
Thou God!
 
Join Date: May 2003
Location: Barnsley, UK.
Posts: 116
Checking if a username exists

Hi
I need a way to check a mysql database for a certain username to see if it exists, i tried using both
PHP Code:
if(mysql_query("SELECT username FROM ut_users WHERE username='$username'"))
and

PHP Code:
if(!mysql_query("SELECT username FROM ut_users WHERE username='$username'"))
The first dosnt check and the second always says that it found a username.

Any other ways to check?

Thanks.
djdaz is offline   Reply With Quote
Old 12-06-2003, 08:37 AM   #2
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,479
That's because you're getting a result set from mysql_query(). You still need to look inside that to see what the result was.
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Weedpacket is offline   Reply With Quote
Old 12-06-2003, 10:19 AM   #3
selesfan87
Junior Member
 
Join Date: Dec 2003
Posts: 3
you could:

Run your query
Use the mysql_num_rows() function to see how many rows where returned.
If 0 were returned, safe to register it.
If more than 0 returned, username is already present in the database.

eg:

$query = "SELECT username FROM user WHERE username='$username'";
$result = mysql_query($query,$dbconnectionvar);
$isregistered = mysql_num_rows($result);

if ($isregistered != 0) { echo "Username taken!"; exit; } else {
// insert code and processing
echo "Username registered!"; }
selesfan87 is offline   Reply With Quote
Old 12-06-2003, 11:19 AM   #4
seby
Senior Member
 
Join Date: Nov 2002
Posts: 476

something like:

PHP Code:
$checkuser = mysql_query("SELECT username FROM ut_users WHERE username='$username'");

if(
mysql_num_rows($checkuser) == 0)
{
     echo
'that username is not taken';
}
else
{
     echo
'sorry, that username is taken';
}
__________________
Counter-Strike Planet

Last edited by seby; 12-06-2003 at 11:23 AM.
seby is offline   Reply With Quote
Old 12-06-2003, 11:25 AM   #5
djdaz
Thou God!
 
Join Date: May 2003
Location: Barnsley, UK.
Posts: 116
ahh, now i see

Thanks people.
djdaz is offline   Reply With Quote
Old 12-06-2003, 01:31 PM   #6
ahundiak
Bright Member
 
ahundiak's Avatar
 
Join Date: Aug 2002
Posts: 1,608
And be sure to create an unique index on username and check the error codes when inserting a new one. It's still possible for duplicate usernames to slip past your code in a multi-user enviroment.

In fact, with a unique index you can avoid the initial check all together.
ahundiak is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 12:38 PM.






Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.