Page 1 of 1

make sure user name exists in the database

Posted: Fri Feb 18, 2005 6:31 am
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

Posted: Fri Feb 18, 2005 6:37 am
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;

Posted: Fri Feb 18, 2005 6:58 am
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

Posted: Fri Feb 18, 2005 7:06 am
by n00b Saibot
This script is alright. You are somewhat confused :? :?

Posted: Fri Feb 18, 2005 7:11 am
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

Posted: Fri Feb 18, 2005 7:17 am
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;

Posted: Fri Feb 18, 2005 7:31 am
by Jim_Bo
Hey,

Thats what im after ..


Thanks

Posted: Fri Feb 18, 2005 10:16 am
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?