开发者

Weird PHP problem

I have the following code...

&开发者_JAVA技巧lt;?php
  session_start();
  require_once('myDB.php');
  if (isset($_POST))
  {
     foreach($GLOBALS['myDB']->getShops()as $i)
     {
       $tempLat = $i['latitude'];
       $templong  = $i['longitude'];
       $distance = distance($_POST['latitude'],$_POST['longitude'],$tempLat,$templong);
       if($distance <= 300)
       {
          echo "".$i['id']."|".$i['shopName']."|".$i['longitude']."|".$i['latitude']."|".$i['advert']."|";
       }      
     }
  }
  function distance($lat1, $lon1, $lat2, $lon2) {
  $theta = $lon1 - $lon2;
  $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
  $dist = acos($dist);
  $dist = rad2deg($dist);
  $miles = $dist * 60 * 1.1515;
  return (($miles * 1.609344)*1000);
 }
?>

Now when I get to see that page it gives me Error 324 (net::ERR_EMPTY_RESPONSE) and it says that the website may be temporarily down...If i try to open the page using firefox it opens a dialog for downloading the php file...

What I would like to do is to have it print either the echoes (if distance is in fact bigger than 300 meters) or nothing at all but it should still be an empty html page...

Can anyone see whats wrong with that???

NOTE: The code works fine locally but not on my server..however every other page works as expected so the server is up and running...


If i read your code right, you are only going to get a response from that if $distance is > 300. Something to consider.


The answer is to put an ELSE statement after your IF. You'll see it's just printing an HTML skeleton.

Obviously, you'll want a doctype declaration and maybe a nice message saying "No results", but you get the idea...

<?php
  session_start();
  require_once('myDB.php');
  if (isset($_POST))
  {
     foreach($GLOBALS['myDB']->getShops()as $i)
     {
       $tempLat = $i['latitude'];
       $templong  = $i['longitude'];
       $distance = distance($_POST['latitude'],$_POST['longitude'],$tempLat,$templong);
       if($distance <= 300)
       {
          echo "".$i['id']."|".$i['shopName']."|".$i['longitude']."|".$i['latitude']."|".$i['advert']."|";
       }      
     }
  }
else{
  print '<html><Head><title></title></Head><body></body></html>';
}
  function distance($lat1, $lon1, $lat2, $lon2) {
  $theta = $lon1 - $lon2;
  $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
  $dist = acos($dist);
  $dist = rad2deg($dist);
  $miles = $dist * 60 * 1.1515;
  return (($miles * 1.609344)*1000);
 }
?>

If you're still getting an error, something is wrong in your include (you'll need to post that), or something in your IF is throwing an exception.

Do you have error reporting on? Have you checked your webserver log? Have you tried wrapping the suspect operations in a try/catch?


Ok after commenting out and uncommenting each line one by one I found where the problem is...

 foreach($GLOBALS['myDB']->getShops()as $i)

The query in the database was failing because of the type of a field set to longtext...

Makes no sense at all but changing that to varchar fixed the problem...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜