Solved

How to use mysql_num_rows to check if username has been used?

Posted on 2009-07-03
3
682 Views
Last Modified: 2012-05-07
I think I'm missing something simple on the code below, but I can't find the problem.  This is for a registration form and I'm just trying to query the table (users) and see if the username has already been used.  If the username has already been taken then tell the user, if the username hasn't been taken register the user.  What am I missing here?
// check the username and make sure it hasn't been taken
$result = mysql_query ("SELECT username from users WHERE username = '$new_user_name'");
$num_rows = mysql_num_rows($result);
 
if ($num_rows = '1') {
	echo "That username is already in use, please select another username! <a href='add_user.php'>Try Again</a>";
}
if ($num_rows = '0') {
		 mysql_query ("INSERT INTO users (first_name, last_name, username, password, email, state_id, state)
		 VALUES ('$first_name', '$last_name', '$user_name', '$encrypted_password', '$email', '$state_id', '$state_name')");	
         echo 'Good news, the user was added!';
      }

Open in new window

0
Comment
Question by:jbpeake
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 11

Expert Comment

by:BrianMM
ID: 24772787
Is it doing anything?

try:
// check the username and make sure it hasn't been taken
$result = mysql_query ("SELECT username from users WHERE username = '$new_user_name'");
 
if($result) {
	if(mysql_num_rows($result) > 0) {
		echo "That username is already in use, please select another username! <a href='add_user.php'>Try Again</a>";
	} else {
		// Insert...
		mysql_query ("INSERT INTO users (first_name, last_name, username, password, email, state_id, state)
                 VALUES ('$first_name', '$last_name', '$user_name', '$encrypted_password', '$email', '$state_id', '$state_name')");     
		echo 'Good news, the user was added!';
	}
} else {
	echo "Sorry problem with query:".mysql_error();
}

Open in new window

0
 
LVL 5

Accepted Solution

by:
Opalelement earned 500 total points
ID: 24772811
I would agree with the "if($var > 0) else" method, as it means there will always be a Yes or No result, even if there happens to be more than 1.

However, in your code I am guessing you always got both?

The problem is with these:
if ($num_rows = '1')
if ($num_rows = '0')

You need another = sign in both of those, like this:
if ($num_rows == '1')
if ($num_rows == '0')

From http://www.php5-tutorial.com/basics/if-statements/
As you can see, we are using two equal signs after each other - why is that? In PHP, and in many other programming languages, a single equal sign is an assignment operator. It's used to assign a value to e.g. a variable. Two equal signs are a comparison operator, that is, it's used to compare two values. So, in this case, we use two equal signs to compare the two items: $number and 42. Had we only used one equal sign, 42 would be assigned to $number, and no comparison would have been made.
0
 
LVL 11

Expert Comment

by:BrianMM
ID: 24772847
yes... that is correct also... should have spotted that in original code :)

General practice of using if(mysql_num_rows($result) > 0) is 'safer' incase for any reason there is more than one.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql select total by week ending 3 26
Error when Running powershell script 6 31
add if statement powershell 8 25
Dump data from mysql to xls php 10 25
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question