blitz Posted May 28, 2013 Share Posted May 28, 2013 (edited) Hello phpfreaks!, I know the title is really confusing. But let me explain. I need a little help here. $queryreport = mysql_query("SELECT post_time_dif FROM response WHERE profile_id = '$id'"); while($row = mysql_fetch_array($queryreport)){ $post_time_dif = $row['post_time_dif']; $post_time_dif_tot += $post_time_dif; } Basically what I want to do is add all values from post_time_dif and put the total in $post_time_dif_tot variable Running the above code gives me an error Notice: Undefined variable: post_time_dif_tot Im relatively new @php so any help or idea would really be appreciated. Thanks Edited May 28, 2013 by blitz Quote Link to comment https://forums.phpfreaks.com/topic/278463-adding-tables-while-mysql_fetch_array/ Share on other sites More sharing options...
PravinS Posted May 28, 2013 Share Posted May 28, 2013 $queryreport = mysql_query("SELECT post_time_dif FROM response WHERE profile_id = '$id'"); $post_time_dif_tot = 0; while($row = mysql_fetch_array($queryreport)){ $post_time_dif = $row['post_time_dif']; $post_time_dif_tot += $post_time_dif; } set zero value to $post_time_dif_tot variable above while loop or try above code or else you can disable notice errors from php.ini file Quote Link to comment https://forums.phpfreaks.com/topic/278463-adding-tables-while-mysql_fetch_array/#findComment-1432686 Share on other sites More sharing options...
blitz Posted May 28, 2013 Author Share Posted May 28, 2013 Hmm. Thanks, it seemed to work on numbers. But it returns 0 when adding time. Basically i would like to get the sum of all records in post_time_dif and get the average. Quote Link to comment https://forums.phpfreaks.com/topic/278463-adding-tables-while-mysql_fetch_array/#findComment-1432690 Share on other sites More sharing options...
Barand Posted May 28, 2013 Share Posted May 28, 2013 What format / column type is post_time_dif? Quote Link to comment https://forums.phpfreaks.com/topic/278463-adding-tables-while-mysql_fetch_array/#findComment-1432696 Share on other sites More sharing options...
blitz Posted May 28, 2013 Author Share Posted May 28, 2013 It was originally TIME, it got me in a lot of trouble so i changed it to VARCHAR. And it brought me a lot lot more trouble hahaha. Anyway I kinda got the work around already. I insert all record returns to an array and then using array_sum divide it by count of the array. and some more modifications based on my spagehetti code hehehe Thanks everyone! Quote Link to comment https://forums.phpfreaks.com/topic/278463-adding-tables-while-mysql_fetch_array/#findComment-1432710 Share on other sites More sharing options...
mac_gyver Posted May 28, 2013 Share Posted May 28, 2013 if you check your result, it is only using the leading field in the TIME value (the hours.) you need to actually use a function that can perform math on a time value, generally by converting to a single base unit (seconds), performing the math, then converting back to a TIME value. Quote Link to comment https://forums.phpfreaks.com/topic/278463-adding-tables-while-mysql_fetch_array/#findComment-1432715 Share on other sites More sharing options...
Barand Posted May 28, 2013 Share Posted May 28, 2013 if you leave it as a time field SELECT SEC_TO_TIME(AVG(TIME_TO_SEC(post_time_dif))) as tot FROM response WHERE profile_id = '$id' job done Quote Link to comment https://forums.phpfreaks.com/topic/278463-adding-tables-while-mysql_fetch_array/#findComment-1432720 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.