我已经提出了一个查询,以确定一个房间的每人总费用以及该房间的总人数;每人的总费用正确返回,房间总人数也是如此;然而,当我试图回应总数的次要乘法时,我只是显示我希望乘以的数字。
例如: 每人价格= 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 />";
}
答案 0 :(得分: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 />";
}
答案 1 :(得分: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 />";
}
答案 2 :(得分: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完成。