在PHP中显示mysql SUM

时间:2015-03-29 13:43:35

标签: php mysql sum inner-join

我正在尝试在PHP中输出MySQL查询,之前我一直在使用此语句来打印echo("<td>$line[meal_name]</td></tr>"); 在我的下面的代码中,我尝试这样做,但我收到错误,我正在努力找出原因

$query = 'SELECT SUM(meal.calorific_output), SUM(activity.calorific_output)
                            FROM meal
                            INNER JOIN activity
                            ON meal.customer_id=activity.customer_id AND meal.day=activity.day
                            WHERE meal.customer_id =
                "'. $_POST['customer_id'] .'"AND meal.day = "'. $day.'"';
            $result = mysql_query($query) or die('Query failed: ' . mysql_error());

            // Printing results
            while ($line = mysql_fetch_array($result)) {

                echo"<tr><td>$day</td>";
                echo("<td>$line[SUM(meal.calorific_output)]</td>");
                                echo("<td>$line[SUM(meal.calorific_output)]</td></tr>");

1 个答案:

答案 0 :(得分:0)

您应该为列指定名称吗?比如SELECT SUM(meal.calorific_output) as sumMeal,

    $query = 'SELECT SUM(meal.calorific_output) as sumMeal, SUM(activity.calorific_output) as sumActivity
                            FROM meal
                            INNER JOIN activity
                            ON meal.customer_id=activity.customer_id AND meal.day=activity.day
                            WHERE meal.customer_id =
                "'. $_POST['customer_id'] .'"AND meal.day = "'. $day.'"';
            $result = mysql_query($query) or die('Query failed: ' . mysql_error());

            // Printing results
            while ($line = mysql_fetch_array($result)) {

                echo"<tr><td>$day</td>";
                echo("<td>$line["sumMeal"]</td>");
                                echo("<td>$line["sumActivity"]</td></tr>");