Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/254201-intermitent-array-problem/
Share on other sites

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>>';
      }

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...

$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);

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.