【问题标题】:simple calculation of field for php mysql fieldphp mysql 字段的简单字段计算
【发布时间】:2014-06-14 22:17:21
【问题描述】:

我有一个简单的数据库,它是由页面中的表单填写的,在条目中一切正常。

我在数据库中有 2 个字段:all_students 和 current_students 由用户以我要计算的形式填写

诀窍是我只在输出页面中回显最新的数据库记录.. 只给我插入表单中的最新数据...

现在,我想创建一个新字段,自动为我提供缺席学生(所有 - 当前)

我已经尝试过,我读到我不能在数据库中创建一个新的计算字段,它不是一个 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);

?>

【问题讨论】:

  • 用减号删除句点

标签: php mysql database calculated-field


【解决方案1】:

如果你想连接一个字符串,你只需要放点 (.)。由于是减法,请去掉点。

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

【讨论】:

  • 那不起作用,请注意,这两个字段是 db 中的 varchar,它给出没有标签的负值!只给-40!奇怪..但我也怀疑 $row 没有在查询中选择,因为它是计算出来的,我只回显数据库中唯一的最后一条记录,如查询所示,这有关系吗?怎么了?
  • @MikeDerik,如果你回显 $row['all_studnets'] 和 $row['current_studnets'].. 你得到什么?您是否从数据库中获得了正确的信息?
  • 是的,个别字段工作正常,提取正常。
  • 另外,如果我回显“缺席学生:”。 $row['all_students' - 'current_students']。 "
    ";给我记录的id号!!!我回应“缺席的学生:”。 $row['所有学生网'] - $row['current_students']。 "
    ";它给出与 (current_student) 值相同的负值,但为负值,如 -40
  • 完美,很有效 :) 非常感谢亲爱的,非常感谢 :),虽然效果很好,但我无法投票赞成你的答案
【解决方案2】:

试试这条线

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

这个

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-08
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多