我试图弄清楚如何从MySQL查询中获得正确的结果,该查询包括当输出编码为json时的求和函数。
当没有sum函数时,结果会正确返回(这对我来说也可能是一个问题,但是我还没有尝试过)但是当MySQL查询中有一个sum函数时它只有返回一个结果/对象(我假设它是总和的总和)。
这是我第一次使用mysql_fetch_array()来检索数组结果,以及如何在网上实现json_encode的所有示例,我发现使用此函数。 mysql_fetch_array()能否与sum和GROUP BY函数一起使用?
{
$sql = mysql_query("SELECT portfolio,agency,program,objectives_text,last,current FROM `table` WHERE MATCH(program,objectives_text) AGAINST('health'IN BOOLEAN MODE) ");
$results = array();
while($row = mysql_fetch_array($sql))
{
$results[] = array(
'portfolio' => $row['portfolio'],
'agency' => $row['agency'],
'program' => $row['program'],
'objectives_text' => $row['objectives_text'],
'component' => $row['component'],
'last' => $row['last'],
'current' => $row['current']
);
}
$json = json_encode($results);
echo
"$json";
}
//the above code produces correct results in json output with integer value for each row correct
{
$sql = mysql_query("SELECT portfolio,agency,program,objectives_text,last,current,sum(last),sum(current) FROM `table` WHERE MATCH(program,objectives_text) AGAINST('health'IN BOOLEAN MODE) ");
$results = array();
while($row = mysql_fetch_array($sql))
{
$results[] = array(
'portfolio' => $row['portfolio'],
'agency' => $row['agency'],
'program' => $row['program'],
'objectives_text' => $row['objectives_text'],
'component' => $row['component'],
'last' => $row['sum(last)'],
'current' => $row['sum(current)']
);
}
$json = json_encode($results);
echo
"$json";
}
//produces one result/object in the json output with the integer results being a sum of the individual rows