从mysql获取Xcode中的显示值错误?

时间:2014-02-26 09:37:15

标签: php mysql json xcode

嗨我想从mysql中获取值以在Xcode textfield中显示。但是我在文本字段中得到(null)值。 我的Xcode ......

- (IBAction)find:(id)sender {
    NSError *err;
    NSString *strURL=[NSString stringWithFormat:@"http://192.168.1.15:81/priya/sam.php?FirstName=%@",name.text];
    NSData *dataURL=[NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
    NSString *strResult=[[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
    NSLog(@"got response==%@", strResult);
    NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:dataURL options: NSJSONReadingMutableContainers error:&err];
    NSArray *array=[jsonArray objectForKey:@"key"];
    address.text=[NSString stringWithFormat:@"%@", [[array objectAtIndex:0]objectForKey:@"firstName"]];
    phone.text=[NSString stringWithFormat:@"%@", [[array objectAtIndex:1]objectForKey:@"lastname"]];
}

和我的PHP代码..

<?php

$host = "localhost"; 
$user = "xcode"; 
$pass = "xcode"; 
$db="xcode";
$r = mysql_connect($host, $user, $pass);
echo mysql_get_server_info() . "\n"; 
$r2 = mysql_select_db($db);
$result1 = mysql_query("SELECT * FROM Persons WHERE FirstName='{$_GET['FirstName']}'");
$result=array();
while($row = mysql_fetch_array($result1))
    array_push($result,array('firstName' => $row[0],'lastname'=>$row[1]));

echo json_encode(array("key" => $result));

mysql_close();
?>

我也在控制台中获得了JSON值

 {"key":[{"firstName":"Peter","lastname":"Griffin"},{"firstName":"Peter","lastname":"Griffin"}]}

帮我找到答案。提前谢谢。

0 个答案:

没有答案