Search code examples
phpiosnsstringnsdata

getting wrong int value from string


I am running a php script on my server via an iOS app that checks details entered into UITextFields and allows user to log in. Th response back from the PHP script is simply

   <?php require_once("includes/connection_auth.php"); 
?>
<?php


    $username = $_GET["userName"];
    $userpassword = $_GET["userPassword"];

    //check if users username exists in database
    $selectQuery = mysqli_query($connection, "SELECT * FROM MVUsers WHERE userName = '$username' ");
    if (!$selectQuery) {
        printf("Error: %s\n", mysqli_error($connection));
        exit();
    }


    if ($row = mysqli_fetch_assoc($selectQuery)){
        //user found in database check password matches



         if($row ['userPassword'] == $userpassword){
            //password matches check if confirmed email
                if($row ['userHasConfirmedEmail'] == 1){
                     //allow user to log in
                    echo (0);
                  }
                  else{
                  echo (1);

                 }

          }
          else{
            //password does not match warn user

            echo (2);
               }    

    }



    else{

    echo(3);

   }
    //close connection to database
    mysqli_close($connection);

?>

This echoed number is returned as NSData into my app and inside one of the NSURLConnectionDataDelegate callbacks i convert that data to an NSString then get the intValue from that string.

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *string = [[NSString alloc]initWithData:[self receivedData] encoding:NSUTF8StringEncoding];

NSLog(@"Connection finished loading with string :%@",string);

int returnedInt = [string intValue];
 NSLog(@"%i",returnedInt);

// returnedInt is the compared inside a switch statement. 
}

The two log statements have different outputs for same data.

2014-02-05 13:20:47.363 MotoVlogger[50944:70b] Connection finished loading with string :






3
2014-02-05 13:20:47.363 MotoVlogger[50944:70b] 0

Why is is 3 in one instance and 0 in the other also why is all there all that added white space in the first log statement?


Solution

  • The string has a bunch of newlines; strip those out and you should get the expected intValue