在我的应用程序中,用户可以发布内容,其他人可以对此发表评论。无论如何,要查看帖子,您可以在表格视图中看到其标题,然后将其推送到详细信息视图。该segue带有职位ID。要显示评论,我必须将帖子ID发送到php文件。然后,我根据该帖子ID从数据库中提取数据,并在json数组中回显它们,以便目标c可以读取并显示它。我的问题是我永远不会回显数据,因为发布ID不会传输到php文件,或者发生了其他事情。
我在其他文件中使用以下相同的目标c代码来发送数据并将其插入数据库,并且工作正常,因此我不确定问题出在哪里。
这是我的目标c:
-(void) getData:(NSData *) data{
NSError *error;
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
}
-(void) start{
NSMutableString *postString = [NSMutableString stringWithString:kRecieveUrl];
[postString appendString:[NSString stringWithFormat:@"?%@=%@", kId, _post_id]];
[postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
[request setHTTPMethod:@"POST"];
postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
NSURL *url = [NSURL URLWithString:kRecieveUrl];
NSData *data = [NSData dataWithContentsOfURL:url];
[self getData:data];
}
和我的PHP:
<?php
$db_connect = mysql_connect("localhost", "root", "")
or die("Our servers are down at the moment");
mysql_select_db("My DB") or die("Couldnt find db");
$post_id = $_GET['id'];
$query = "SELECT * FROM Comments WHERE post_id='$post_id'";
$results = mysql_query($query);
$num = mysql_numrows($results);
mysql_close();
$rows = array();
while($r = mysql_fetch_assoc($results))
{
$rows[] = $r;
}
echo json_encode($rows);
?>
最佳答案
尝试这样的事情
- (void)start {
NSMutableString *postString = [NSMutableString stringWithString:RecieveUrl];
[postString appendString:[NSString stringWithFormat:@"?%@=%@", kId, _post_id]];
[postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue currentQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {
// do error handling
} else {
id json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (error) {
// do error handling
} else {
// do something with json
}
}
}];
}
关于php - 将数据发送到php文件,并根据该数据无法正常工作,从数据库中提取内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31055324/