将iphone应用程序连接到mysql数据库
我有一个应用程序我想连接到iphone应用程序我已经做了代码和php代码的问题是,我总是得到不正确的密码警报视图。我正在输入正确的用户名和密码,但它再次显示错误警报视图将iphone应用程序连接到mysql数据库,iphone,objective-c,Iphone,Objective C,我有一个应用程序我想连接到iphone应用程序我已经做了代码和php代码的问题是,我总是得到不正确的密码警报视图。我正在输入正确的用户名和密码,但它再次显示错误警报视图 NSString *post =[NSString stringWithFormat:@"UserName=%@&UserPassword=%@",userNameTextField.text, userPasswordTextFiled.text]; NSString *hostStr = @"
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);
?>
尝试使用以下代码
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);
此链接可能会帮助您这是我的链接亲爱的,但我有相同的问题,我得到的字符串中的服务器输出是@“”;只有这样,这就是为什么我认为它给出了错误,你能帮我解决这个问题吗?你从服务器得到的响应是字符串“serverOutput”?@;这是当我登录时,但当我直接给php脚本提供用户名和密码值并与数据库匹配时,它返回了是的,我给你这个结果只是尝试回显你的查询,看看它响应了什么?这与我告诉你的打印空间相同,只是因为有@“”在strI中,我对php了解不多,我认为php代码中存在一些问题?意味着您试图从服务器回显的任何内容都是空白的?