我有一个名为 mysql 的数据库和一个名为 的表。该表有 6 列:waterparamsdbwaterparamsTemperatureAcidityAmmoniaNitriteNitratePhosphate我使用HTML表单用数字更新表格。这部分工作正常。在 PHPMYADMIN 中,我看到包含以下信息的新行:temperature ¦ acidity ¦ ammonia ¦ nitrite ¦ nitrate ¦ phosphate 25.50 ¦ 7.60 ¦ 0.25 ¦ 0.00 ¦ 0.00 ¦ 1.00 26.00 ¦ 7.80 ¦ 0.50 ¦ 0.25 ¦ 0.25 ¦ 2.00我遵循 https://www.w3schools.com/php/php_mysql_select.asp 示例,并能够首先使用以下方法获取信息:<!DOCTYPE html><html><head><meta content="text/html; charset=utf-8" http-equiv="Content-Type"><title>Untitled 1</title></head><body><?php $username = "redacted"; $password = "redacted"; $database = "waterparamsdb"; $mysqli = new mysqli("localhost", $username, $password, $database); $query = "SELECT * FROM waterparams";echo '<table border="0" cellspacing="2" cellpadding="2"> <tr> <td> <font face="Arial">Temperature</font> </td> <td> <font face="Arial">Acidity</font> </td> <td> <font face="Arial">Ammonia</font> </td> <td> <font face="Arial">Nitrite</font> </td> <td> <font face="Arial">Nitrate</font> </td> <td> <font face="Arial">Phosphate</font> </td> </tr>';if ($result = $mysqli->query($query)) { while ($row = $result->fetch_assoc()) { $field1name = $row["col1"]; $field2name = $row["col2"]; $field3name = $row["col3"]; $field4name = $row["col4"]; $field5name = $row["col5"]; $field5name = $row["col6"]; echo '<tr> <td>'.$field1name.'</td> <td>'.$field2name.'</td> <td>'.$field3name.'</td> <td>'.$field4name.'</td> <td>'.$field5name.'</td> <td>'.$field6name.'</td> </tr>'; } $result->free();} ?></body></html>这工作正常,并以纯格式显示两行信息。我想做的是只将最后一行的值输出到HTML表中,并在网页上显示信息,如下所示:Temperature ¦ ValueAcidity ¦ ValueAmmonia ¦ ValueNitrite ¦ ValueNitrate ¦ ValuePhosphate ¦ Value我已经尝试了从上面的网站改编的以下内容,但它返回0结果。
1 回答
慕容森
TA贡献1853条经验 获得超18个赞
像这样更改 if 循环
if ($result->num_rows > 0) {
echo "<table><tr><th>Temperature</th><th>Acidity</th><th>Ammonia</th><th>Nitrite</th><th>Nitrate</th><th>Phosphate
</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["Temperature"]. "</td><td>" . $row["Acidity"]."</td><td>" . $row["Ammonia"]."</td><td>" . $row["Nitrite"]."</td><td>" . $row["Nitrate"]."</td><td>" . $row["Phosphate"]."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
但请确保这些数组中的值应是正确的列名$row["Temperature"]
并将 sql 查询更改为此查询
$sql = "SELECT * FROM waterparams";
- 1 回答
- 0 关注
- 85 浏览
添加回答
举报
0/150
提交
取消