显示来自php的数据

时间:2014-04-17 10:30:09

标签: ios objective-c

我想用php文件中的数据填充标签或文本字段。我的代码在下面我的xcode上有一条错误消息,说自动释放不可用。这是正确的方法吗?我该如何解决这个错误?

以下是我的php代码

<?php
include( "./inc/header.inc.php");
include ("./inc/connect.inc.php");
?>
<?php

request=mysql_query("SELECT * FROM TEAM WHERE TeamID = '9'");
if($request){
$count = mysql_num_rows($request);

if ($count==1){
    $rows = mysql_fetch_array($request);
    $score = $rows['TeamName'];

    echo"$score";
}
}
?>

这是我在viewdid加载部分中的xcode代码

NSString *strURL = [NSString stringWithFormat:@"www.rugbycoachanalysis.com/applogintesting.php"];

    // to execute php code
    NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];

    // to receive the returned value
    NSString *strResult = [[[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding]autorelease];
    _labelTest.text =strResult;
}

由于

3 个答案:

答案 0 :(得分:4)

您可能正在使用禁止手动发布的ARC。所以你不能释放或自动释放。

NSString *strResult = [[[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding]autorelease];

应该是

NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];

除此之外。

您的代码中还有其他一些错误。您正在使用dataWithContentOfURL调用同步请求,该请求将阻止该线程。另外stringWithString也是一种浪费,因为你可以简单地将你的字符串声明为

NSString *strURL = @"www.rugbycoachanalysis.com/applogintesting.php"];

更新..尝试新代码以检查是否还有错误

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: strURL];
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response,NSData *data,NSError *error){
    if (error == nil) {
        NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"result %@",string);
    }
    else {
        NSLog(@"error in request %@",error);
    }
}];

答案 1 :(得分:1)

您正在使用Automatic Reference Counting (ARC)。所以你不必同时关于内存释放。移除autorelease它将消除错误。

    NSString *strURL = [NSString stringWithFormat:@"www.rugbycoachanalysis.com/applogintesting.php"];

    // to execute php code
    NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];

    // to receive the returned value
    NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
    _labelTest.text =strResult;
}

答案 2 :(得分:1)

使用JSON对数据进行编码,将其传输到iPhone应用并从那里解码。

你的php代码应该是

<?php

request=mysql_query("SELECT * FROM TEAM WHERE TeamID = '9'");
if($request){
$count = mysql_num_rows($request);

if ($count==1){
    $rows = mysql_fetch_array($request);
    $score = $rows['TeamName'];

    echo json_encode($score);
}
}
?>

目标C代码应该是

NSString *strURL = [NSString stringWithFormat:@"www.rugbycoachanalysis.com/applogintesting.php"];

    // to execute php code
    NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];

    // to receive the returned value
    NSError *error = nil;
    id object = [NSJSONSerialization
                      JSONObjectWithData:dataURL
                      options:0
                      error:&error];
 NSLog(@"%@",object);