php-mysql字段的简单计算


simple calculation of field for php mysql field

我有一个简单的数据库,它由页面中的一个表单填充,条目中的所有内容都可以。

我在数据库中有两个字段:all_students和current_students,由用户在我想要计算的表单中填写

诀窍是,我只在输出页面中回显最新的db记录。。只给我插入表格中的最新数据…

现在,我想创建一个新的领域,给我缺席的学生自动(所有当前)

我试过的,我读到我不能在数据库中创建一个新的计算字段,它不是excel,所以我试图将这两个字段的计算结果回声到一个新值,即"缺席学生"

你有什么建议?请帮忙,这是我的代码

<?php
$con=mysqli_connect("localhost","root","PasswordHere","DBnameHere");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$result = mysqli_query($con,"SELECT id, class, all_students, current_students FROM students ORDER BY id DESC LIMIT 1");

while($row = mysqli_fetch_array($result))
  {
  echo "Class: " . $row['class'] . "<br>";
  echo "All students: " . $row['all_studnets'] . "<br>";
  echo "Current students: " . $row['current_studnets'] . "<br>";
  echo "Absent students: " . $row['all_studnets'] . - . $row['current_studnets'] . "    <br>";
  }

mysqli_close($con);
?>

如果要连接字符串,则只需放置句点(.)。既然你在做减法,请去掉这个点。

 echo "Absent students: ";
 echo floatval($row['all_studnets'])  -  floatval($row['current_studnets']);
 echo "<br>";

试试这行

echo "Absent students: " . $row['all_studnets']  -  $row['current_studnets'] . "    <br>";

本的说明

echo "Absent students: " . $row['all_studnets'] . - . $row['current_studnets'] . "    <br>";