Question

i have simple db that it is filled by a form from a page, all is ok in the entry.

i have 2 fields in the database are: all_students and current_students filled by the user in the form that i want to calculate

the trick is that i am echoing only the latest db record in the output page.. to give me only the latest data inserted in the form...

now, i want to create a new field that give me the absent students automatically (all - current)

what i have tried, i read that i can NOT create a new calculated field in the db, it is not an excel, so i am trying to echo the calculation results of these 2 fields, to a new value that is the "absent students"

what you suggest? please help, here is my code

<?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);

?>
Was it helpful?

Solution

You only put dot (.) if you want to concatenate a string. Please remove the dot since you are subtracting.

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

OTHER TIPS

Try this line

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

Insted of this

echo "Absent students: " . $row['all_studnets'] . - . $row['current_studnets'] . "    <br>";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top