我正在尝试将我的用户名发送到服务器。这是我的objective-c代码:
NSURL *postUrl=[NSURL URLWithString:@"http://yatanadam.com/Nappsak/checkTheUser.php"];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:postUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0f];
NSString *strData=@"userName=yatanadam";
NSData* data = [strData dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d",[data length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:data];
NSURLResponse *response=nil;
NSError *error=nil;
NSData *result=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *string = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
if (error==nil) {
NSLog(@"%@",string);
}
特此我的php代码:
<?php
$userName = $_POST["userName"];
echo $userName ;
$con = mysql_connect("localhost","username","password");
if (!$con)
{
$hata= "hata";
echo hata;
die('Baglanti saglanamadi: ' . mysql_error());
}mysql_select_db("yata", $con);
$degisken= "Select * from Users where user_name=$userName";
$showresult = mysql_query($degisken);
$multi_array = array();
while($row = mysql_fetch_assoc($showresult)){
$multi_array[] = $row;}
print json_encode($multi_array);
?>}
我在这个问题上已经好几个小时了。问题是我无法从服务器获得正确的响应。我的服务器代码无法捕获我的帖子数据(用户名)。我有任何人可以帮助我,我的错误在哪里?