foreach看起来不像在php中正常工作

时间:2014-03-02 10:59:08

标签: php sql foreach

我使用以下代码来获取总价格,但它输出的值高于预期值。这是什么原因?

$query =  " SELECT *
            FROM `mytable`
            WHERE `sale_id` = $id";

$result = mysql_query($query);
$item =  mysql_fetch_array($result);


foreach ($item as $row) {            
    $total_price += $row['price']; 
}

echo $total_price;

1 个答案:

答案 0 :(得分:1)

试试这个,

$result = mysql_query($query);

while ($row = mysql_fetch_array($result)) {            
    $total_price += $row['price']; 
}

echo $total_price;