Page 1 of 1

comparing php mysql strings

Posted: Mon Jul 12, 2010 11:21 am
by Josh_Dude83
I'm having a heck of a time getting the code to evaluate this function the way I want it. This is my first real php project so please ignore n00b mistakes and things that could be done simpler. Still learning. If you look at my code you can see all the echo's I've been trying to use to debug. The issue is the if(rtrim(trim($row['userID'])) == rtrim(trim($userid))) I can't get it to evaluate to true no mater what I do. ==, ===,trims, strcmp. None of them seem to work. The userID is in the database which I manage and the right userid is being compared against it. WHAT IS WRONG!! :banghead:
Here is the output of the echo's:
Connected to database!
inside create user
here is user ID: test1
This is the query: SELECT userID FROM USERS WHERE userID = 'test1'
This is the result of query: test1
this is userid: test1
in the else
You are a new user!
Thank you for joining our website.
Invalid new user insert Duplicate entry 'test1' for key 1

So here is my function's code:

Code: Select all

function create_user($userid, $password, $email, $security1, $answer1, $security2, $answer2){
	//establish connection to database
	$db = mysql_connect('localhost', 'username', 'password');
   		if($db): 
       		mysql_select_db('PogoersckeyJ'); 
       		echo "Connected to database!<br />";
   		else:  
       		die ("Could not connect to db! " . mysql_error());        
   		endif;
	
	
	echo "inside create user<br />";
	echo "here is user ID: ".$userid."<br />";
	
	//possible that user forget's password and doesn't realize their account already exists
	//check to see if the user id already exists in the database
	
	$sql= "SELECT userID FROM USERS WHERE userID = '$userid'";
	echo "This is the query: $sql <br />";
	#$sql= sprintf("SELECT userID FROM USERS WHERE userID = '%s'", $userid);	
	
	$result = mysql_query($sql) or die ("Error searching for user id's, Invalid: " . mysql_error());
	
	while($row = mysql_fetch_array($result)){
  		echo "This is the result of query: ".$row['userID'];
  		echo "<br />";
  	}
	
	echo "this is userid: $userid <br />";
	
	//compare the id's if equal user exsits, false it is a new user to the database
	if(rtrim(trim($row['userID'])) == rtrim(trim($userid))){
		
			echo "not in the if";
		
		   #unset($GLOBALS[$_SESSION["option"]]);
           #unset($_SESSION["info"]);
           #unset($_SESSION["history"]);
           session_unset();
           //session_destroy();
           
		mysql_close($db);
		echo "The account already exists, please user a different User ID!<br />";
		echo "You will be re-directed to New User page in 5 seconds... <br /><br />";
		header( 'refresh: 10b; url=https://cs1520.cs.pitt.edu/~jap97/mysql/newuser.php');
		#return false;
		
	
	}
	else{
		echo "in the else<br />";
		echo "You are a new user!<br />";
		echo "Thank you for joining our website. <br />";
		
		$md5password = md5($password);
		$md5answer1 = md5($answer1);
		$md5answer2 = md5($answer2);
		
		$query = "INSERT INTO USERS VALUES ('$userid', '$md5password', '$email', '$security1' , '$md5answer1' , '$security2' , '$md5answer2')"; 
		mysql_query($query) or die ("Invalid new user insert " . mysql_error());
		
		   #unset($GLOBALS[$_SESSION["option"]]);
          # unset($_SESSION["info"]);
          # unset($_SESSION["history"]);
          session_unset();
          //session_destroy();
		
       login_user($userid, $password);
       mysql_close($db);
		echo "You will be re-directed to Login page in 5 seconds... <br />";
		header( 'refresh: 5; url=http://cs1520.cs.pitt.edu/~jap97/mysql/textexchange.php');
		#return true;
		
		
		
	}
}//end of create_user

Re: comparing php mysql strings

Posted: Mon Jul 12, 2010 11:23 am
by Josh_Dude83
I have also tried to no avail:

Code: Select all

if(!empty($row['userID'])){
//and
if(isset($row['userID'])){

Re: comparing php mysql strings

Posted: Mon Jul 12, 2010 12:24 pm
by Jade
Try this:

Code: Select all

function create_user($userid, $password, $email, $security1, $answer1, $security2, $answer2){
        //establish connection to database
        $db = mysql_connect('localhost', 'username', 'password');
                if($db):
                mysql_select_db('PogoersckeyJ');
                echo "Connected to database!<br />";
                else:  
                die ("Could not connect to db! " . mysql_error());        
                endif;
       
       
        echo "inside create user<br />";
        echo "here is user ID: ".$userid."<br />";
       
        //possible that user forget's password and doesn't realize their account already exists
        //check to see if the user id already exists in the database
       
        $sql= "SELECT userID FROM USERS WHERE userID = '$userid'";
        echo "This is the query: $sql <br />";
        #$sql= sprintf("SELECT userID FROM USERS WHERE userID = '%s'", $userid);       
       
        $result = mysql_query($sql) or die ("Error searching for user id's, Invalid: " . mysql_error());
       
        while($row = mysql_fetch_array($result)){
                echo "This is the result of query: ".$row['userID'];
                echo "<br />";
        }
       
        if (mysql_num_rows($result)) //a record already exists for this user
               
                        echo "not in the if";
               
                   #unset($GLOBALS[$_SESSION["option"]]);
          #unset($_SESSION["info"]);
          #unset($_SESSION["history"]);
          session_unset();
           //session_destroy();
           
                mysql_close($db);
                echo "The account already exists, please user a different User ID!<br />";
                echo "You will be re-directed to New User page in 5 seconds... <br /><br />";
                header( 'refresh: 10b; url=https://cs1520.cs.pitt.edu/~jap97/mysql/newuser.php');
                #return false;
               
       
        }
        else{
                echo "in the else<br />";
                echo "You are a new user!<br />";
                echo "Thank you for joining our website. <br />";
               
                $md5password = md5($password);
                $md5answer1 = md5($answer1);
                $md5answer2 = md5($answer2);
               
                $query = "INSERT INTO USERS VALUES ('$userid', '$md5password', '$email', '$security1' , '$md5answer1' , '$security2' , '$md5answer2')";
                mysql_query($query) or die ("Invalid new user insert " . mysql_error());
               
                   #unset($GLOBALS[$_SESSION["option"]]);
         # unset($_SESSION["info"]);
         # unset($_SESSION["history"]);
         session_unset();
          //session_destroy();
               
       login_user($userid, $password);
       mysql_close($db);
                echo "You will be re-directed to Login page in 5 seconds... <br />";
                header( 'refresh: 5; url=http://cs1520.cs.pitt.edu/~jap97/mysql/textexchange.php');
                #return true;
               
        }
}//end of create_user


Re: comparing php mysql strings

Posted: Mon Jul 12, 2010 12:29 pm
by Josh_Dude83
thats it. That works! Thank you so much for the quick reply One thing I didn't realize is that the while loop i have to check the query actually moved the file pointer ahead to do so. So next time I go to check the query result it was actually empty. Learning is fun! :oops:

Re: comparing php mysql strings

Posted: Mon Jul 12, 2010 1:55 pm
by Jade
Haha you mean learning is a pain in the ass but we all appreciate it once the lightbulb finally clicks until we don't use the knowledge for a while and then it's back to stupid square one....