0

问题

我生成了一个查询来确定一个房间的每人总成本和房间内的总人数;每人的总费用和房间里的总人数一样正确返回;然而,当我试图呼应总数的小乘法时,我只是得到了我希望相乘的数字的显示。

例如: 每人价格 = $1207 房间总人数 = 2 查询结果 ... "1207*2" ...

问题:

我敢肯定这是愚蠢或次要的,但可以使用第二双眼睛。如何重写下面的代码以产生$2414的预期结果?

这是查询:

$tourquery = "SELECT t.roomprice, c.totpax 
         FROM clients c, tourprices t 
         WHERE c.roomtype1 = t.roomtype 
         AND c.tourstart = t.tourstart
         AND c.tourbk_id = t.tour_id"; 
$tourresult = $db->query($tourquery); 


while ($roomrate = mysqli_fetch_assoc($tourresult))
   {
echo "Per person amount : {$roomrate['roomprice']}<br />";
echo "Total room amount : {$roomrate['roomprice']}*{$roomrate['totpax']}<br />";

}
4

3 回答 3

2

你为什么不这样做:

while ($roomrate = mysqli_fetch_assoc($tourresult))
{
    $totalcost = $roomrate['roomprice'] * $roomrate['totpax'];
    echo "Per person amount : {$roomrate['roomprice']}<br />";
    echo "Total room amount : {$totalcost}<br />";
}
于 2012-10-23T21:41:34.570 回答
0

你也可以很花哨,让 mySQL 完成数学运算。

SELECT t.roomprice, c.totpax, (t.roomprice * c.totpax) AS total_price 
     FROM clients c, tourprices t 
     WHERE c.roomtype1 = t.roomtype 
     AND c.tourstart = t.tourstart
     AND c.tourbk_id = t.tour_id

不确定您是否会受到性能影响。我最终决定是由数据库完成处理还是由 PHP 完成处理。

于 2012-10-23T21:46:48.287 回答
0

这是因为您正在输出一个字符串。有多种方法可以解决此问题,包括:

while ($roomrate = mysqli_fetch_assoc($tourresult))
   {
$total_price = $roomrate['roomprice']*$roomrate['totpax'];
echo "Per person amount : {$roomrate['roomprice']}<br />";
echo "Total room amount : {$total_price}<br />";

}
于 2012-10-23T21:43:11.633 回答