makka Posted November 17, 2008 Share Posted November 17, 2008 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 [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 [/edit] Quote Link to comment Share on other sites More sharing options...
premiso Posted November 17, 2008 Share Posted November 17, 2008 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. Quote Link to comment Share on other sites More sharing options...
makka Posted November 17, 2008 Author Share Posted November 17, 2008 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... anyway to be honest i don't know what i want atm... bed time i think its been a long day Quote Link to comment Share on other sites More sharing options...
trq Posted November 17, 2008 Share Posted November 17, 2008 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"; } Quote Link to comment Share on other sites More sharing options...
makka Posted November 18, 2008 Author Share Posted November 18, 2008 thanks, it makes seance today when i look at it dunno what happened last night Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.