编辑:我已经成功地计算了我想要获得的值,但不是计算每行的值,而是计算一次并在任何地方发布该值。如何使用我的代码重新计算每一行?
图片:http://img515.imageshack.us/img515/9064/example2w.png
新守则:
<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">
© Copyright Ryan Strouse ©
</div>
</footer>
</html>
我有两个数据库表,我成功地将列数据拉入表中。表格的第三个单元格我想计算数据库中某些列的百分比。我不知道如何对此进行编码...我试图从另一个我发现没有运气的线程中提出SELECT语句中的内容。
以下是我正在努力开展工作的查询图片:http://img696.imageshack.us/img696/3862/examplegw.png
<html>
<head>
<title>PHP-MySQL Project 4</title>
</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);
//this is where I am trying to calculate the value and then below it display in table
//cell # 3
$SQLstring3 = "SELECT *,((resultResponses/surveyResponses)*100) AS AMOUNT FROM surveys, results";
$QueryResult3 = @mysqli_query($DBConnect, $SQLstring3);
do {
echo "<table>";
echo "<tr><th>Commercial</th> <th>Views</th> <th>Percentage</th></tr>";
echo "<tr><td>{$Row['resultDescription']}</td>";
echo "<td>{$Row['resultResponses']}</td>";
echo "<td>$QueryResult3</td></tr>";
$Row = mysqli_fetch_assoc($QueryResult);
} 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>
</footer>
</html>
答案 0 :(得分:1)
IF 我理解您的要求,您可能正在尝试计算在MySQL查询结果中可以找到的值的百分比。如果是这样,那么我将使用函数mysql_num_rows
来获取记录的总数,然后在while
和if
我有一个计数器,我有多少次满足这个价值。
然后你只做简单的数学运算,例如:
result = (100 * counter) / mysql_num_rows
并且有一个百分比。然后你只需要echo
结果,无论你想要什么! :)
我希望我能正确理解你的问题!
答案 1 :(得分:1)
$result = mysql_query("SELECT yourRow FROM yourTable");
$aArray = array();
$cont=0;
while($col = mysql_fetch_assoc($result){
$aArray[$cont] = $col['yourRow'];
cont++;
}
这应该为你提供一个可行的数组,让你用它来计算,然后回应它。干杯