Php 从数据库计算列,然后拉入表单元格

Php 从数据库计算列,然后拉入表单元格,php,mysql,mysqli,Php,Mysql,Mysqli,编辑:我已经成功地计算出了我想要得到的值,但不是为每一行计算该值,而是只计算一次并将该值发布到所有地方。如何使用我的代码重新计算每一行 图片: 新代码: <html> <head> <title>PHP-MySQL Project 4</title> <div align="center"> <p> PHP-MySQL Project 4 <br/>

编辑:我已经成功地计算出了我想要得到的值,但不是为每一行计算该值,而是只计算一次并将该值发布到所有地方。如何使用我的代码重新计算每一行

图片:

新代码:

<html>
<head>
<title>PHP-MySQL Project 4</title>

    <div align="center">
        <p>
            PHP-MySQL Project 4
        <br/>
            By: Ryan Strouse
        </p>
    </div>
</head>
<body bgcolor="#99FFFF">

<?php

$DBName = "surveys";
$DBConnect = @mysqli_connect("localhost", "students", "password")
    Or die("<p>Unable to connect to the database server.</p>"
    . "<p>Error code " . mysqli_connect_errno()
    . ": " . mysqli_connect_error()) . "</p>";

if (!$DBConnect)
    {
    echo "<p> The database server is not available.</p>";
    }
else
    {
    echo "<p> Successfully connected to the database $DBName</p>";
    }

mysqli_select_db($DBConnect, $DBName); 
echo "<p>Database -'$DBName'- found</p>";

$SQLstring = "SELECT * FROM surveys WHERE surveyCode = 'GEI001'";
$QueryResult = @mysqli_query($DBConnect, $SQLstring);
echo $SQLstring;
$row = mysqli_fetch_assoc($QueryResult);

$count_surveys = $row['surveyResponses'];

echo "<p>Total Responses: $count_surveys</p>";


$SQLstring2 = "SELECT * FROM results WHERE surveyCode = 'GEI001'";
$QueryResult2 = @mysqli_query($DBConnect, $SQLstring2);
echo $SQLstring2;
echo "<br/>";
$Row = mysqli_fetch_assoc($QueryResult2);


$SQLstring3 = "SELECT * FROM surveys, results";
$QueryResult3 = @mysqli_query($DBConnect, $SQLstring3);
$fetchrow = mysqli_fetch_assoc($QueryResult3);
$result_amount = (($fetchrow['resultResponses'] / $fetchrow['surveyResponses']) * 100);

echo "<table>";
echo "<tr><th>Commercial</th> <th>Views</th> <th>Percentage</th></tr>";
do {
    echo "<tr><td>{$Row['resultDescription']}</td>";
    echo "<td>{$Row['resultResponses']}</td>";
    echo "<td>$result_amount</td></tr>";
    $Row = mysqli_fetch_assoc($QueryResult3);
} while ($Row);
echo "</table>";


?>



<center>
<h3><a href="Survey1.html">Return To Main Page</a></h3>
<h3><a href="../Menu.html">Return to Menu</a></h3>
</center>
</body>

<footer>
<div align="center">
&copy; Copyright Ryan Strouse &copy;

</div>
</footer>
</html>

PHP MySQL项目4

PHP MySQL项目4

作者:Ryan Strouse


如果我理解您的要求,那么您可能正在尝试计算在MySQL查询结果中可以找到的值的百分比。如果是这样,那么我将使用函数
mysql\u num\u rows
来获取记录的总数,然后在
如果
我将有一个计数器,指示我满足该值的次数

然后你只需做简单的数学运算,例如:

result = (100 * counter) / mysql_num_rows
还有一个百分比。然后您只需
echo
将结果显示在您想要的任何位置

我希望我正确理解了你的问题

$result = mysql_query("SELECT yourRow FROM yourTable");
$aArray = array();
$cont=0;
while($col = mysql_fetch_assoc($result){
   $aArray[$cont] = $col['yourRow'];
   cont++;
}

这应该会给你一个可行的数组,让你用它来做你的数学,然后回应它。干杯

这是含糊不清的,您没有给出任何示例代码,说明您试图计算的百分比。你试过什么?数据库中的数据是什么,等等。@SamuelCook我已经编辑并提供了图片。如果它有助于关闭,我正在尝试根据从单独的表中划分两列来计算百分比。两列都设置为INT。我的SELECT语句返回NULL而不是值。如果有帮助,下面是结果的图片:列
surveyResponses
resultResponses
是否为算术类型?另外,如果您在MySQL结果中得到错误,那么由于您仍然在选择值,因此使用PHP而不是MySQL生成数学公式<代码>$result\u amount=($result['resultResponses']/$result['surveyResponses'])*100获取错误“无法使用mysqli_result类型的对象作为数组”我的代码是$result_amount=($QueryResult3['resultResponses']/$QueryResult3['surveyResponses'])*100;这两列都设置为INT(7),所以我应该能够对它们执行算术。不知道如何使用数组来进行数学运算,你能给我更多的演示吗?你可以使用百分比并从中获得乐趣
$result = mysql_query("SELECT yourRow FROM yourTable");
$aArray = array();
$cont=0;
while($col = mysql_fetch_assoc($result){
   $aArray[$cont] = $col['yourRow'];
   cont++;
}