0
我的php有一個稱爲load的方法。我可以插入不同的方法,但是當我改變參數來說這個方法時,我認爲它正確地發送了數據,但它沒有顯示在我的應用程序中。如何在JSON中編碼MYSQL響應並在iOS應用中解析
的index.php
function load(){
//Establish connection
$con=mysqli_connect("localhost","root","","Mealidea");
// Check connection
if (mysqli_connect_errno()){
$errorR['name']="Failed to connect to mysql".mysqli_connect_error();
echo json_encode(errorR);
}
//Perform query
$result = mysqli_query($con,"SELECT * FROM User");
$myjsons = array();
while($row = mysql_fetch_assoc($result)){
$myjsons[] = $row;
}
//Close connection
mysqli_close($con);
echo json_encode($myjsons);
}
Set請求:
//Get from DB
NSURL*url=[NSURL URLWithString:@"http://localhost/Tutorials/index.php?f=load&key=6dM7V0n5GqYJLTMibQDf2gA2a94h8hbF"];
//URL request
NSURLRequest*request=[NSURLRequest requestWithURL:url];
//Set the connection
connection = [NSURLConnection connectionWithRequest:request delegate:self];
if (connection) {
webData=[[NSMutableData alloc]init];
}
獲取答案:
NSDictionary*data=[NSJSONSerialization JSONObjectWithData:webData options:0 error:&error];
//If data is nil, then print error
if (data==nil) {
//NSLog(@"%@",error);
}
//Print the data
NSLog(@"%@",data);
//???? how to parse data?
NSDictionary*num=[data valueForKey:@"User"];
您是否在問如何讓數據顯示在您的應用程序中? –
我認爲我發送和獲取的內容之間存在錯誤。我應該收到作爲字典的應用程序?這些數據是如何構成的? (本週我剛剛閱讀了關於PHP的文章)。 PHP發送字典或數組? –
請參閱下面的答案 –