I want to calculate the BMI of a member and save it in the database. They can then just view their stored BMI.
Below is the only thing that I can came up with but there's some erro开发者_开发百科r in the codes (mysql_fetch_array() expects parameter 1 to be resource, boolean given in).
Could someone help me fix it?
$bmi= mysql_query ("SELECT id WHERE ((SELECT (weight)) / ((SELECT (height)) * (SELECT (height))) FROM myMembers) FROM myMembers");
while($row = mysql_fetch_array($bmi)){
$sqlUpdate = mysql_query("UPDATE myMembers SET bmi='$bmi' WHERE id='$id'");
}
SELECT id, (weight / height * height) as bmi FROM myMembers
The deal with SQL is you want to reduce the number of quires as much as possible. sub-selects should be avoided, but its a lot better than breaking it up into a ton of queries. doing a while() over a select should be avoided like herpes.
update myMember set bmi=weight/(height*height)
And this will set the entire column for all users. But really this is a hack it should be done on insert or with a trigger.
精彩评论