从mysql db读取值以平均行

时间:2012-04-17 22:58:22

标签: php mysql xcode json

-(int)getRatings:(int)idNo
{ 

NSString *urlString = [NSString stringWithFormat:@"http://www.website.com/rating-grab.php"];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error;
NSMutableArray *json = (NSMutableArray*)[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];



NSLog(@"json output \n:  %@  \n",json);
NSLog(@"______________\n”);

/// not sure how to access the value
NSLog(@"object at: %@\n",[json objectAtIndex:0]);
}

-----在php里面

// How to output values of more than one column at a time?
$sth = mysql_query("SELECT AVG(overall) FROM votes WHERE idNo=1");

$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows[] = $r;
}
echo json_encode($rows);

php output   [{"AVG(overall)":"5.00000”}]

Xcode output

json output 
:  (
        {
        "AVG(overall)" = "5.00000";
    }
)  
2012-04-17 15:54:12.202

我希望能够将每列的平均值存储为变量。我似乎越来越近了(当然不是最有效的方式)。

1 个答案:

答案 0 :(得分:1)

您是否有理由无法修改SQL查询以获取同一请求中多列的平均值,例如: SELECT AVG(overall),AVG(othercolumn1),AVG(othercolumn2),AVG(othercolumn3) FROM ...