sprint10s Posted March 30, 2009 Share Posted March 30, 2009 below is the code that i wrote and it still works but i get a warning error on the site. basically im trying to select all the rows in a racing event of in the case below the dash for cash race and take the results of the race and show an average start and a average finish of each event. but if that user does not race in the dash and puts a 0 or null value i get the following error Warning: Division by zero in /home/www/standridgemotorsports.com/func/db_results_body.php on line 105 i think i need another if statement to say if total is 0 then print 0 or something but not sure how to do it. Thanks in advance. //calculates average Dash Start $ds = mysql_query("SELECT d_start FROM track WHERE user_id=1 "); $numds = 0; while($datads = mysql_fetch_array($ds)) { if($datads['d_start']!=0 && $datads['d_start']!=""){ $numds++; //This line is a shortcut that basically means $numhf=$numhf+1 } $totalds += $datads['d_start']; } $avgds = round($totalds/ $numds, 2); //end of Dash Start Quote Link to comment https://forums.phpfreaks.com/topic/151824-solved-division-by-0-error/ Share on other sites More sharing options...
Renlok Posted March 30, 2009 Share Posted March 30, 2009 before $avgds = round($totalds/ $numds, 2); add $numds ? ($numds == 0) ? 1 : $numds; Quote Link to comment https://forums.phpfreaks.com/topic/151824-solved-division-by-0-error/#findComment-797202 Share on other sites More sharing options...
sprint10s Posted March 30, 2009 Author Share Posted March 30, 2009 tried that then get a Parse error: syntax error, unexpected ';' on the line that i added Quote Link to comment https://forums.phpfreaks.com/topic/151824-solved-division-by-0-error/#findComment-797215 Share on other sites More sharing options...
Maq Posted March 30, 2009 Share Posted March 30, 2009 $numds = ($numds == 0) ? 1 : $numds; Quote Link to comment https://forums.phpfreaks.com/topic/151824-solved-division-by-0-error/#findComment-797218 Share on other sites More sharing options...
sprint10s Posted March 30, 2009 Author Share Posted March 30, 2009 Sweet it worked thanks guys for the quick response and help, I really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/151824-solved-division-by-0-error/#findComment-797227 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.