将iphone应用程序连接到mysql数据库

问题描述:

我有应用程序我想连接到iphone应用程序我已经完成代码和PHP代码问题是,我总是得到不正确的密码警报视图。我AAM输入正确的用户名和密码,但再次显示了错误警报视图将iphone应用程序连接到mysql数据库

 NSString *post =[NSString stringWithFormat:@"UserName=%@&UserPassword=%@",userNameTextField.text, userPasswordTextFiled.text]; 

    NSString *hostStr = @"http://www.myurl.com/emrapp/connect.php?"; 
    hostStr = [hostStr stringByAppendingString:post]; 
    NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];  
    NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding]; 
    if([serverOutput isEqualToString:@"Yes"]){ 
     UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Congrats" message:@"You are authorized " 
                  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
    [alertsuccess show]; 
    [alertsuccess release]; 


    } else { 
     UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Username or Password Incorrect" 
                  delegate:self  cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
     [alertsuccess show]; 
     [alertsuccess release]; 

    } 

而且我的服务器端代码

<?php 
    $con = mysql_connect("emriphone.db.6420177.hostedresource.com","emriphone","Light12-"); 
    if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

    mysql_select_db("emriphone", $con); 

    $u=$_GET['UserName']; 
    $pw=$_GET['UserPassword']; 


    $query = sprintf("SELECT UserName,UserPassword from appUsers WHERE UserName='%s' AND UserPassword='%s'", mysql_real_escape_string($u),mysql_real_escape_string($pw)); 



    $login=mysql_query($query,$con) or die(mysql_error()); 


    if(mysql_num_rows($login)==1){ 

    $row =mysql_fetch_assoc($login); 
    echo 'YES'; exit; 
    } 

    else{ 
    echo'NO';exit; 
    } 

    mysql_connect($con); 


    ?> 
+0

这个链接可以帮助你http://*.com/questions/2720288/how-to-connect-with-sqlite-in-iphone – 2012-03-19 07:19:09

+0

这是我的链接亲爱的,但我有同样的问题 – user1263350 2012-03-19 07:28:11

+0

字符串中的服务器输出我越来越是@“”;只有所以这就是为什么我认为它给错误可以帮助我在这s – user1263350 2012-03-19 07:56:49

尝试使用下面的代码

  NSString *data = [[NSString stringWithFormat:@"UserName=%@&UserPassword=%@",userNameTextField.text, userPasswordTextFiled.text]; 

      NSData *postData = [data dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

    // preaparing URL request to send data. 
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 

NSString *url = [NSString stringWithFormat:@"http://www.myurl.com/emrapp/connect.php?"]; 

      [request setURL:[NSURL URLWithString:url]]; 
      [request setHTTPMethod:@"POST"]; 
      [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
      [request setHTTPBody:postData]; 
      [request setTimeoutInterval:7.0]; 

NSURLResponse *response;// = [[NSURLResponse alloc] init]; 
NSError *error;// = [[NSError alloc] init; 

NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

    NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; 


    NSLog(@"Login response:is %@",str); 
+0

对你有用吗? – Rupesh 2012-03-19 09:47:29

+0

与第一次不工作相同 – user1263350 2012-03-19 10:00:48

+0

NSLOg中的输出是什么? – Rupesh 2012-03-19 10:01:40