如何从表中提取值,计算它们并在页面上显示结果

时间:2013-06-04 13:41:52

标签: php mysql risk-analysis

enter image description here

嗨,我正在根据上面的图片(表格)进行风险分析编码。不幸的是,我不知道如何将公式转换为编码并在php页面上显示结果。基本上,该公式涉及每行的可能性*严重性的乘法,在每个行之间相加,然后对其求平均值(除以3)。简而言之

result1 = (likelyhood1 * severity1)
result2 = (likelyhood2 * severity2)
result3 = (likelyhood3 * severity3)

a = (result1 + result2 + result3) / 3

结果“a”随后显示在同一个php页面上(我正在使用mysql数据库)。

我确实试着寻找答案,但无济于事。这就是我被困住的地方。

<?php

$con=mysqli_connect("localhost","root","","risk");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM Persons");

//$res1 = 

mysqli_close($con);

?>

2 个答案:

答案 0 :(得分:3)

您可以直接查询:

SELECT SUM(likelyhood * severity) / COUNT(*) FROM Persons

编辑:

甚至更简单:

SELECT AVG(likelyhood * severity) FROM Persons

答案 1 :(得分:2)

$total = 0;
while($row=mysqli_fetch_array($result)){
    $total += $row['likelyhood'] * $row['severity'];
}
$a = $total/3;