2cool Posted March 30, 2009 Share Posted March 30, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/151784-calculations-in-php/ Share on other sites More sharing options...
Mchl Posted March 30, 2009 Share Posted March 30, 2009 $S1Weight='S1Weight'; You're asigning a string to a variable... how do you expect to get any results? And this second method... just don't think about it anymore... Quote Link to comment https://forums.phpfreaks.com/topic/151784-calculations-in-php/#findComment-797008 Share on other sites More sharing options...
Maq Posted March 30, 2009 Share Posted March 30, 2009 You also posted this in the wrong section (do not start a new one), you probably meant to put this in the "PHP Math" section. Also, please use tags for you code. Quote Link to comment https://forums.phpfreaks.com/topic/151784-calculations-in-php/#findComment-797014 Share on other sites More sharing options...
2cool Posted March 31, 2009 Author Share Posted March 31, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/151784-calculations-in-php/#findComment-797634 Share on other sites More sharing options...
sasa Posted March 31, 2009 Share Posted March 31, 2009 <?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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/151784-calculations-in-php/#findComment-797724 Share on other sites More sharing options...
2cool Posted March 31, 2009 Author Share Posted March 31, 2009 Thanks sasa, I have entered the amended code but it gives this error: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource Quote Link to comment https://forums.phpfreaks.com/topic/151784-calculations-in-php/#findComment-797744 Share on other sites More sharing options...
lonewolf217 Posted March 31, 2009 Share Posted March 31, 2009 that probably means your query is failing <?php $result=mysql_query($query) or die (mysql_error()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/151784-calculations-in-php/#findComment-797750 Share on other sites More sharing options...
Maq Posted March 31, 2009 Share Posted March 31, 2009 Just a wild guess, but did you mean "clientID" rather than "clientD"? (notice the I) Quote Link to comment https://forums.phpfreaks.com/topic/151784-calculations-in-php/#findComment-797754 Share on other sites More sharing options...
2cool Posted March 31, 2009 Author Share Posted March 31, 2009 Silly me, I think I've spent too much time on it...I have corrected the spelling and it works fine now. Thank you all for the help. Quote Link to comment https://forums.phpfreaks.com/topic/151784-calculations-in-php/#findComment-797759 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.