make sure user name exists in the database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

make sure user name exists in the database

Post by Jim_Bo »

Hey,

This checks if the user name allready exists in the databse .. How do you do the opposite and make sure the user name exists or die

Code: Select all

$sql_username_check = mysql_query("SELECT user_name FROM users WHERE user_name='$user_name'");
 
$username_check = mysql_num_rows($sql_username_check);
 
 	if($username_check > 0){
 		echo "<b>Error</b> Username doesnt exist";
 		echo "<p><a href="../index.php?pages=form">BACK</a></p>";	
 
 	return;  
 &#125;

Thanks
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Is it me or does your question not make any sense

what's wrong with what you have? This will check if no rows are returned spit out an error, else, do whatever you need with the username

Code: Select all

$sql_username_check = mysql_query("SELECT user_name FROM users WHERE user_name='$user_name'"); 
  
$username_check = mysql_num_rows($sql_username_check); 
  
 if($username_check == 0)
 &#123; 
       echo "<b>Error</b> Username doesnt exist
             <p><a href="../index.php?pages=form">BACK</a></p>";    
 &#125;
 else
 &#123;
       //do what you want with unique username
 &#125;
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hey,

It doesnt make sence ..

I need it to check that the username exists .. if it does carry on and exicute the script .. if it doesnt exist fail and show the form again ..

The posted code does the following .. it fails if the username exists and exicutes the script if it doesnt ..

Thanks
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

This script is alright. You are somewhat confused :? :?
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

hey,

Nope im not confused ..

If the user name exists the script fails ..

If the user name doesnt exist .. it carrys on and exicutes the rest of the script ..

I want it to check that the user name exists .. if it does .. carry on with the script .. If the user name doesnt exist in the database .. then stop the script and show error message ..

Thanks
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

OK Maybe this is what you want

Code: Select all

$sql_username_check = mysql_query("SELECT user_name FROM users WHERE user_name='$user_name'"); 
  
$username_check = mysql_num_rows($sql_username_check); 

if($username_check == 0) 
&#123; 
  die("<b>Error</b>: Username doesn't exist .<p>
 <a href="../index.php?pages=form">BACK</a></p>");
 &#125;
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Post by Jim_Bo »

Hey,

Thats what im after ..


Thanks
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

die() is typically used when something is happening that shouldn't. The code I did for you will not execute anything in the else statement if no user is found.

Why do you need the script to terminate?
Post Reply