Jump to content

Calculations in php


2cool

Recommended Posts

Hi

I'm tyring to do this calculation, shown below:

 

$Height='Height';

$S1Weight='S1Weight';

$result= ($Height * $Height / $S1Weight);

echo ($result);

 

Another way i tried was:

 

print ("BMI for Session 1 is: ");

print ("S1Weight FROM `main` WHERE clientID='1'" * "S2Weight FROM `main` WHERE clientID='1'" / "Height FROM `main` WHERE clientID='1'");

 

It gives me an error: 'Division by zero', There are figures in all the fields i am using ,i.e. Height and S1Weight and i am trying to individualise to to clientID. Any ideas why it throws this error?

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/151784-calculations-in-php/
Share on other sites

What i am trying to do is use the fields from my database (S1Weight and Height) to work out this calculation. I am a new programmer and not sure if I'm doing this the right way. I have tried many different ways but none have been successful. The code i am using now is:

 

<?php 
$query="SELECT Height FROM `main` WHERE clientD='1'"; 
$result=mysql_query($query); 
$row = mysql_fetch_assoc($result); 
echo $row['Height'];
?>
<br>
<?php
$query="SELECT S1Weight FROM `main` WHERE PatientID='1'"; 
$result=mysql_query($query); 
$row1 = mysql_fetch_assoc($result); 
echo $row1['S1Weight']; 
?>
<br>
<?php
$c=($row*$row/$row1);
echo $c;
?>

 

The first two parts display the Height and S1Weight individually but when i put it into the calculation i need i throws an error, 'Fatal error: Unsupported operand types'.

Firstly is this the correct code and if it is how could i go about resolving this error. If not, can you direct me the right way.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/151784-calculations-in-php/#findComment-797634
Share on other sites

<?php 
$query="SELECT Height FROM `main` WHERE clientD='1'"; 
$result=mysql_query($query); 
$row = mysql_fetch_assoc($result); 
echo $h = $row['Height'];
?>
<br>
<?php
$query="SELECT S1Weight FROM `main` WHERE PatientID='1'"; 
$result=mysql_query($query); 
$row1 = mysql_fetch_assoc($result); 
echo $w = $row1['S1Weight']; 
?>
<br>
<?php
$c=$w!=0 ? ($h*$h/$w) : 'S1Weight is zero!';
echo $c;
?>

 

Link to comment
https://forums.phpfreaks.com/topic/151784-calculations-in-php/#findComment-797724
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.