2014-02-05 69 views
0

得到错误的int值,我通过一个iOS应用程序,检查进入UITextFields细节,并允许用户登录运行我的服务器上的PHP脚本。Th反应回简直是从字符串

<?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); 

?> 

这个回显的数字作为NSData返回到我的应用程序,并在NSURLConnectionDataDelegate回调之一,我将该数据转换为NSString然后从该字符串中获取intValue。

- (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. 
} 

这两个日志语句对于相同的数据具有不同的输出。

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 

为什么3是在一个实例中,0在另一个为什么所有在第一个日志语句中添加空白的所有?

+0

您的php无效。 – njzk2

+0

对不起我sudo编码我会更新与实际的PHP。这将改变我原来的帖子 – RyanTCB

+0

你也可以删除“mysql”标签。这似乎与数据库无关。 –

回答

2

该字符串有一堆换行符;剥离出来的那些,你应该得到预期的intValue

+0

使用string = [string stringByReplacingOccurrencesOfString:@“”withString:@“”]修复它。 string = [string stringByReplacingOccurrencesOfString:@“\ n”withString:@“”]; string = [string stringByReplacingOccurrencesOfString:@“\ r”withString:@“”]; – RyanTCB

0

试试这个:

-(NSInteger)convert:(NSString *)str 
{ 
    str =[str stringByReplacingOccurrencesOfString:@" " withString:@""]; 
    NSInteger returnedInt = [str integerValue]; 
    NSLog(@"%ld",(long)returnedInt); 
    return (long)returnedInt; 
} 


-(int)convert:(NSString *)str 
{ 
    str =[str stringByReplacingOccurrencesOfString:@" " withString:@""]; 
    int returnedInt = [str intValue]; 
    NSLog(@"%i",returnedInt); 
    return returnedInt; 
} 
+0

used string = [string stringByReplacingOccurrencesOfString:@“”withString:@“”];但获得相同的输出 – RyanTCB

0

看到,因为我需要的字符就在结束我只是用

string =[string substringFromIndex:[string length]-1]; 

的作品,但会喜欢知道空白的原因

编辑:正如奥斯汀所建议的,我不得不剥去很多 的代码,在他的回答下这样做。