merylvingien Posted January 2, 2012 Share Posted January 2, 2012 Hi Folks, wonder if anyone has any clue to what is happening here. 24. $sqlarray = ("SELECT * FROM table WHERE id='$id'"); 25. $resultarray = mysql_query($sqlarray,$con)or trigger_error('<p>There seems to be a problem, please try again soon.</p>'); 26. while($rowarray=mysql_fetch_assoc($resultarray)){ 27. $lat[] = $rowarray['lat']; 28. $long[] = $rowarray['long']; 29. } 30. $averagelat= array_sum($lat) / count($lat); 31. $averagelong= array_sum($long) / count($long); Every now and again i get an error with this code, but it is intermitent and doesnt always happen which is confusing when trying to work out why it is happening. PHP Warning: array_sum() expects parameter 1 to be array, null given in /home/blah/blah on line 30 PHP Warning: Division by zero in /home/blah/blah on line 30 PHP Warning: array_sum() expects parameter 1 to be array, null given in /home/blah/blah on line 31 PHP Warning: Division by zero in /home/blah/blah on line 31 I would assume that on some pages the array is empty but i have checked the database and all fields are occupied and i have also checked that none of them are null. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/254201-intermitent-array-problem/ Share on other sites More sharing options...
requinix Posted January 2, 2012 Share Posted January 2, 2012 The arrays wouldn't be empty - there would be no arrays at all. $lat and $long are only defined if the loop runs, and if there aren't any matching $ids in the database then the loop will not run... Quote Link to comment https://forums.phpfreaks.com/topic/254201-intermitent-array-problem/#findComment-1303293 Share on other sites More sharing options...
jcbones Posted January 2, 2012 Share Posted January 2, 2012 For some reason your query is returning 0 rows, thereby not ever making the array. You need to find out exactly under what circumstances this happens. I would surmise that the $id isn't being set properly (is empty). Check the arrays, and throw in an echo on it if it isn't set. $sqlquery = "SELECT * FROM table WHERE id='$id'"; 24. $sqlarray = ($sql); 25. $resultarray = mysql_query($sqlarray,$con)or trigger_error('<p>There seems to be a problem, please try again soon.</p>'); 26. while($rowarray=mysql_fetch_assoc($resultarray)){ 27. $lat[] = $rowarray['lat']; 28. $long[] = $rowarray['long']; 29. } if(is_array($lat) && is_array($long)) { $averagelat= array_sum($lat) / count($lat); $averagelong= array_sum($long) / count($long); } else { echo $sqlquery . ' <<has failed>>'; } Quote Link to comment https://forums.phpfreaks.com/topic/254201-intermitent-array-problem/#findComment-1303294 Share on other sites More sharing options...
merylvingien Posted January 2, 2012 Author Share Posted January 2, 2012 Thanks for the replies, the script will only run if there are matching id's in the database, or it will run a different script. That all works ok. This only happens every now and again, so trying to echo out the problem isnt really going to help. I have brain ache over this problem as 99% of the time it works just as it should do. On a semi ded server too, so there are no preformance problems etc... Quote Link to comment https://forums.phpfreaks.com/topic/254201-intermitent-array-problem/#findComment-1303307 Share on other sites More sharing options...
jcbones Posted January 2, 2012 Share Posted January 2, 2012 So your problem is, the script checks to see if the id is in the database, but then doesn't pass it to the second query. Yep, no need to run any error checking. Quote Link to comment https://forums.phpfreaks.com/topic/254201-intermitent-array-problem/#findComment-1303308 Share on other sites More sharing options...
Andy-H Posted January 2, 2012 Share Posted January 2, 2012 $sqlarray = ("SELECT * FROM table WHERE id='$id'"); $resultarray = mysql_query($sqlarray,$con)or trigger_error('<p>There seems to be a problem, please try again soon.</p>'); if ( !mysql_num_rows($resultarray) ) { echo 'No results returned for '. (int)$id; exit; } while($rowarray=mysql_fetch_assoc($resultarray)){ $lat[] = $rowarray['lat']; $long[] = $rowarray['long']; } $averagelat= array_sum($lat) / count($lat); $averagelong= array_sum($long) / count($long); Quote Link to comment https://forums.phpfreaks.com/topic/254201-intermitent-array-problem/#findComment-1303372 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.