Jump to content

Recommended Posts

hey there i kinda feal stupid asking this in the 1st place since normally i can do this with no hitch 1st time, but today i cant think to save my life

 

more or less i just want to check to see if a username has been taken

 


function dose_user_exist($username)
{
db_connect();

$query = "SELECT id FROM users WHERE username = '$username'";
$result = mysql_query($query);
if (mysql_num_rows($result) == 0)
{
	return true;
}
else
{
	return false;
}
}

$username = 'Makka';
if(dose_user_exist($username))
{
echo 'yes';
}
else
{
echo 'no';
}

 

if im having a thick day just slap me and kick me in the right direction please  :D

 

[edit]

okey is this just me and this thing is actually working i really can't tell... im going to leave this for tonight since im clearly not in the right mind to do it and ill come back tomorrow and see how much of a idiot ive been :P

[/edit]

Link to comment
https://forums.phpfreaks.com/topic/133128-solved-checking-user/
Share on other sites

Your if statement is checking the wrong one. Change the function to this:

 

function dose_user_exist($username)
{
   db_connect();
   
   $query = "SELECT id FROM users WHERE username = '$username'";
   $result = mysql_query($query);
   if (mysql_num_rows($result) == 0)
   {
      return false;
   }
   else
   {
      return true;
   }
}

 

Your if for dose_user_exist was returning true for if the user did not exist and false if he did exist.

Link to comment
https://forums.phpfreaks.com/topic/133128-solved-checking-user/#findComment-692309
Share on other sites

yeah i think that's what i wanted.. more or less to see if the username has been taken and if it has it would return false and if its free it would return true

 

That logic doesn't fit with the actual name of the function. user_exists would be a more suitable name IMO and it would return true if the user exists or false otherwise.

 

function user_exists($username) {

  db_connect();

  $username = mysql_real_escape_string($username);
  
  $query = "SELECT id FROM users WHERE username = '$username'";
  if ($result = mysql_query($query)) {
    return mysql_num_rows($result);
  
  }

}

 

Now look at how much more sense the calling code makes....

 

if (user_exists('thorpe')) {
  echo "Sorry but that name is already in use";
}

Link to comment
https://forums.phpfreaks.com/topic/133128-solved-checking-user/#findComment-692354
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.