-1

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

My Code:

<?php 
$Sql="SELECT *, (3959 * acos(cos(radians(37)) * cos(radians(44)) * cos(radians(55) -    radians(-122)) + sin(radians(37)) * sin(radians(44))) as distance FROM TableName       HAVING distance < 25 ORDER BY distance LIMIT 0 , 20";
$result=mysql_query($Sql); 

while ($row = mysql_fetch_array($result)){
   echo $row['Id'];
}

Error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home...

Hi, I have longitude and latitude stored in the database and I want to calculate the KM - distance based on the user's current longitude and latitude with the shop's longitude and latitude stored in database. And I want a sorted result as well based on the distance. I googled and found some solutions but getting weird error.. Please check my code. I am not using variable in query. I just want to test if it runs. Please help

Community
  • 1
  • 1
akhan
  • 73
  • 3
  • 10
  • 3
    change this line `$result=mysql_query($Sql);` to `$result=mysql_query($Sql)or die(mysql_error());` and check your error – Miqdad Ali Sep 27 '12 at 04:33
  • I have replaced it with $result=mysql_query($Sql)or die(mysql_error()); but now i can't see any error and i can't see any result – akhan Sep 27 '12 at 05:04

3 Answers3

1

I found new it should be like this

(3959 * acos(cos(radians(37)) * cos(radians(44)) * cos(radians(55) -    radians(-122)) + sin(radians(37)) * sin(radians(44))))

You are missing ")"

Gautam3164
  • 27,319
  • 9
  • 56
  • 81
1

No of opening parentheses is not equal to no of closing parentheses in your SQL statement.

Try this...

SELECT *, (
        3959 * acos(
                 cos(radians(37)) * cos(radians(44)) * 
                 cos(radians(55) -  radians(-122)) + 
                 sin(radians(37)) * sin(radians(44))
                )
       )
    as distance FROM TableName HAVING distance < 25 ORDER BY distance LIMIT 0 , 20
Ashwini Agarwal
  • 4,755
  • 2
  • 38
  • 58
  • Ya I missed the closing parentheses but now I can't see any error the page is blank. However I am putting this code but can't get anything – akhan Sep 27 '12 at 04:54
  • 1
    may be there is not any record in your table full-filling this criteria. check `mysql_num_rows($result)`. – Ashwini Agarwal Sep 27 '12 at 05:10
  • If I just write Select * from tableName. Then it shows the result but If I put my above query for sorting then it doesn't show any error or any record – akhan Sep 27 '12 at 05:17
  • that means that you don't have any record that is under a distance of 25. Your code is correct. try some other values instead of 25. – Ashwini Agarwal Sep 27 '12 at 05:19
0

If you are storing points in your database, you might consider using: http://dev.mysql.com/doc/refman/5.1/en/spatial-extensions.html if your MySQL version is 5.0.16 or later.

An example here: http://howto-use-mysql-spatial-ext.blogspot.com/

rx80
  • 156
  • 4