Trium918 Posted April 24, 2007 Share Posted April 24, 2007 Problem: write a basic script that will check if username is unique. This script works but I am puzzled by the fact the script only works like it is. Why? Note: Just ideas <?php if ($reg_result) // will not work if ($reg_result == true) // will not work if ($reg_result == "true") { echo "<p class=\"genmed\">Your registration was successful. Go to the members page " ."to start setting up your bookmarks!</p>"; } ?> <?php // check if username is unique $user_name = $_POST['user_name']; $result = mysql_query("select * from members_info where user_name='$user_name'"); if (!$result) return "Could not execute query". mysql_error(); if (mysql_num_rows($result)>0) return "<p class=\"genmed\">That username is taken - go back and choose another one.</p>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/48520-solved-check-if-username-is-unique/ Share on other sites More sharing options...
shaunrigby Posted April 24, 2007 Share Posted April 24, 2007 How do you mean the script only works "like this", what do you change to make it "not" work? Quote Link to comment https://forums.phpfreaks.com/topic/48520-solved-check-if-username-is-unique/#findComment-237372 Share on other sites More sharing options...
Trium918 Posted April 24, 2007 Author Share Posted April 24, 2007 How do you mean the script only works "like this", what do you change to make it "not" work? Sorry about that if ($reg_result) // will not work if ($reg_result == true) // will not work Quote Link to comment https://forums.phpfreaks.com/topic/48520-solved-check-if-username-is-unique/#findComment-237376 Share on other sites More sharing options...
sanfly Posted April 24, 2007 Share Posted April 24, 2007 when using true or false, you should use 3 = eg: if ($reg_result === true) But anyway, where do you come up with $reg_result, I dont see it anywhere else in the code you supplied. Quote Link to comment https://forums.phpfreaks.com/topic/48520-solved-check-if-username-is-unique/#findComment-237378 Share on other sites More sharing options...
Trium918 Posted April 24, 2007 Author Share Posted April 24, 2007 when using true or false, you should use 3 = eg: if ($reg_result === true) But anyway, where do you come up with $reg_result, I dont see it anywhere else in the code you supplied. <?php // attempt to register $reg_result = register($user_name,$first_name,$last_name,$gender,$birth_month,$birth_day, $birth_year,$contact_number,$email_address,$password,$street_address, $city_county, $state, $postal_code); if ($reg_result == "true") { echo "<p class=\"genmed\">Your registration was successful. Go to the members page " ."to start setting up your bookmarks!</p>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/48520-solved-check-if-username-is-unique/#findComment-237392 Share on other sites More sharing options...
shaunrigby Posted April 24, 2007 Share Posted April 24, 2007 if($reg_result === TRUE){ //INSERT PROCESSING HERE } else { //UL DUMBA$$ SOMEBODY HAS YOUR PREFERRED NAME } Quote Link to comment https://forums.phpfreaks.com/topic/48520-solved-check-if-username-is-unique/#findComment-237397 Share on other sites More sharing options...
sanfly Posted April 24, 2007 Share Posted April 24, 2007 I take it somewhere in your register function when registration is successful you have return true; If not, you better post your function If so, did you try the 3= ? if ($reg_result === true) Quote Link to comment https://forums.phpfreaks.com/topic/48520-solved-check-if-username-is-unique/#findComment-237400 Share on other sites More sharing options...
MadTechie Posted April 24, 2007 Share Posted April 24, 2007 do a <?php var_dump($reg_result); die; if ($reg_result == "true") ?> i think your find register() is returning a string not a boolean, as for the ===, this forces the check to be of the same type ie if(1=="1") // works if(1==="1")//fails Quote Link to comment https://forums.phpfreaks.com/topic/48520-solved-check-if-username-is-unique/#findComment-237404 Share on other sites More sharing options...
shaunrigby Posted April 24, 2007 Share Posted April 24, 2007 do a <?php var_dump($reg_result); die; if ($reg_result == "true") ?> i think your find register() is returning a string not a boolean, as for the ===, this forces the check to be of the same type ie if(1=="1") // works if(1==="1")//fails that is if s/he uses return true; and in what context (1/0, true/false) Quote Link to comment https://forums.phpfreaks.com/topic/48520-solved-check-if-username-is-unique/#findComment-237406 Share on other sites More sharing options...
Trium918 Posted April 24, 2007 Author Share Posted April 24, 2007 do a <?php var_dump($reg_result); die; if ($reg_result == "true") ?> i think your find register() is returning a string not a boolean, as for the ===, this forces the check to be of the same type ie if(1=="1") // works if(1==="1")//fails You're right it is returning a string! <?php return "<p class=\"genmed\">That username is taken - go back and choose another one.</p>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/48520-solved-check-if-username-is-unique/#findComment-237409 Share on other sites More sharing options...
MadTechie Posted April 24, 2007 Share Posted April 24, 2007 yeah but as shaunrigby pointed out when using booleans === is important as <?php $test="false" if($test) // will pass if($test == true) // will pass if($test === true) // will fails ?> Quote Link to comment https://forums.phpfreaks.com/topic/48520-solved-check-if-username-is-unique/#findComment-237410 Share on other sites More sharing options...
Trium918 Posted April 24, 2007 Author Share Posted April 24, 2007 Well, I got this var_dump($reg_result); die; result = NULL Quote Link to comment https://forums.phpfreaks.com/topic/48520-solved-check-if-username-is-unique/#findComment-237412 Share on other sites More sharing options...
Trium918 Posted April 24, 2007 Author Share Posted April 24, 2007 yeah but as shaunrigby pointed out when using booleans === is important as <?php $test="false" if($test) // will pass if($test == true) // will pass if($test === true) // will fails ?> So what you are saying is that it is better to use the method that I already have than if($test === true) // will fails Quote Link to comment https://forums.phpfreaks.com/topic/48520-solved-check-if-username-is-unique/#findComment-237413 Share on other sites More sharing options...
shaunrigby Posted April 24, 2007 Share Posted April 24, 2007 did that appear on your screen? please wrap [ code][ /code] tags around your code to make it easier for us to understand, and also plus post the function to work out why it is returning NULL Quote Link to comment https://forums.phpfreaks.com/topic/48520-solved-check-if-username-is-unique/#findComment-237414 Share on other sites More sharing options...
MadTechie Posted April 24, 2007 Share Posted April 24, 2007 null means nothing returning (i believe thats a unique name) EDIT: wait a minute thats not right!! can you post the register function code please LOL i hope no one said that dumb mistake of mine LOL Quote Link to comment https://forums.phpfreaks.com/topic/48520-solved-check-if-username-is-unique/#findComment-237415 Share on other sites More sharing options...
shaunrigby Posted April 24, 2007 Share Posted April 24, 2007 Lets make it easier for ourselves, Trium, please post the function wrapped in code tags Quote Link to comment https://forums.phpfreaks.com/topic/48520-solved-check-if-username-is-unique/#findComment-237418 Share on other sites More sharing options...
Trium918 Posted April 24, 2007 Author Share Posted April 24, 2007 <?php function register($user_name,$first_name,$last_name,$gender,$birth_month,$birth_day, $birth_year,$contact_number,$email_address,$password,$street_address, $city_county, $state, $postal_code) // register new person with db // return true or error message { // connect to db $conn = db_connect(); if (!$conn) return "Could not connect to database server - please try later."; // check if username is unique $user_name = $_POST['user_name']; $result = mysql_query("select * from members_info where user_name='$user_name'"); if (!$result) return "Could not execute query". mysql_error(); if (mysql_num_rows($result)>0) return "<p class=\"genmed\">That username is taken - go back and choose another one.</p>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/48520-solved-check-if-username-is-unique/#findComment-237423 Share on other sites More sharing options...
Trium918 Posted April 24, 2007 Author Share Posted April 24, 2007 <?php function register($user_name,$first_name,$last_name,$gender,$birth_month,$birth_day, $birth_year,$contact_number,$email_address,$password,$street_address, $city_county, $state, $postal_code) // register new person with db // return true or error message { // connect to db $conn = db_connect(); if (!$conn) return "Could not connect to database server - please try later."; // check if username is unique $user_name = $_POST['user_name']; $result = mysql_query("select * from members_info where user_name='$user_name'"); if (!$result) return "Could not execute query". mysql_error(); if (mysql_num_rows($result)>0) return "<p class=\"genmed\">That username is taken - go back and choose another one.</p>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/48520-solved-check-if-username-is-unique/#findComment-237424 Share on other sites More sharing options...
sanfly Posted April 24, 2007 Share Posted April 24, 2007 so you need to have in there somewhere if (mysql_num_rows($result) == 0){ return true; } Quote Link to comment https://forums.phpfreaks.com/topic/48520-solved-check-if-username-is-unique/#findComment-237426 Share on other sites More sharing options...
MadTechie Posted April 24, 2007 Share Posted April 24, 2007 <?php if (mysql_num_rows($result)>0) { return false; }else{ return true; } ?> <?php // attempt to register $reg_result = register($user_name,$first_name,$last_name,$gender,$birth_month,$birth_day, $birth_year,$contact_number,$email_address,$password,$street_address, $city_county, $state, $postal_code); if ($reg_result === true) { echo "<p class=\"genmed\">Your registration was successful. Go to the members page " ."to start setting up your bookmarks!</p>"; }else{ echo "<p class=\"genmed\">That username is taken - go back and choose another one.</p>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/48520-solved-check-if-username-is-unique/#findComment-237427 Share on other sites More sharing options...
shaunrigby Posted April 24, 2007 Share Posted April 24, 2007 Change : $conn = db_connect(); if (!$conn) return "Could not connect to database server - please try later."; // check if username is unique $user_name = $_POST['user_name']; $result = mysql_query("select * from members_info where user_name='$user_name'"); if (!$result) return "Could not execute query". mysql_error(); if (mysql_num_rows($result)>0) return "<p class=\"genmed\">That username is taken - go back and choose another one.</p>"; To: $conn = mysql_connect($host,$username,$password) or die("Could not connect to database server - please try later." . mysql_error());//Remove . mysql_error() after you have finished developing your code mysql_select_db($dbname) or die(mysql_error()); // check if username is unique // $user_name = $_POST['user_name'];//Why you setting this? you are obtaining it VIA your function, $query = mysql_query("SELECT * FROM `members_info` WHERE `user_name` = '" . $user_name . "'") or die(mysql_error()); $result = mysql_num_rows($query); if ($result > 0 ) { return true; } else { return false; } Quote Link to comment https://forums.phpfreaks.com/topic/48520-solved-check-if-username-is-unique/#findComment-237430 Share on other sites More sharing options...
Trium918 Posted April 24, 2007 Author Share Posted April 24, 2007 Thanks MadTechie, shaunrigby, and sanfly Quote Link to comment https://forums.phpfreaks.com/topic/48520-solved-check-if-username-is-unique/#findComment-237436 Share on other sites More sharing options...
shaunrigby Posted April 24, 2007 Share Posted April 24, 2007 It all work now m8? Quote Link to comment https://forums.phpfreaks.com/topic/48520-solved-check-if-username-is-unique/#findComment-237438 Share on other sites More sharing options...
MadTechie Posted April 24, 2007 Share Posted April 24, 2007 who cares the solve button has been pressed, lets get out of here lol Good to know Trium918 your welcome Quote Link to comment https://forums.phpfreaks.com/topic/48520-solved-check-if-username-is-unique/#findComment-237462 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.