-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzipcodequery.php
More file actions
executable file
·57 lines (47 loc) · 2.01 KB
/
Copy pathzipcodequery.php
File metadata and controls
executable file
·57 lines (47 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/php
<?php
$dbname = ;
$dbuser = ;
$dbpass = ;
$mysql_handle = mysql_connect('myzipdb.cech3cllvxv3.us-west-2.rds.amazonaws.com:3306', $dbuser, $dbpass)
or die("Error connecting to database server");
mysql_select_db($dbname, $mysql_handle)
or die("Error selecting database: $dbname");
echo "Successfully connected to database!\n";
$zip = '90010';
$d = 10;
$sql = "SELECT * FROM zipcode WHERE zip_code= '$zip'";
$retval = mysql_query( $sql, $mysql_handle );
if(! $retval )
{
die('Could not create table: ' . mysql_error());
}
else {
//if found, set variables
$row = mysql_fetch_array($retval);
$lat1 = $row['lat'];
$lon1 = $row['lon'];
//earth's radius in miles
$r = 3959;
//compute max and min latitudes / longitudes for search square
$latN = rad2deg(asin(sin(deg2rad($lat1)) * cos($d / $r) + cos(deg2rad($lat1)) * sin($d / $r) * cos(deg2rad(0))));
$latS = rad2deg(asin(sin(deg2rad($lat1)) * cos($d / $r) + cos(deg2rad($lat1)) * sin($d / $r) * cos(deg2rad(180))));
$lonE = rad2deg(deg2rad($lon1) + atan2(sin(deg2rad(90)) * sin($d / $r) * cos(deg2rad($lat1)), cos($d / $r) - sin(deg2rad($lat1)) * sin(deg2rad($latN))));
$lonW = rad2deg(deg2rad($lon1) + atan2(sin(deg2rad(270)) * sin($d / $r) * cos(deg2rad($lat1)), cos($d / $r) - sin(deg2rad($lat1)) * sin(deg2rad($latN))));
//find all coordinates within the search square's area
//exclude the starting point and any empty city values
$query = "SELECT * FROM zipcode WHERE (lat <= $latN AND lat >= $latS AND lon <= $lonE AND lon >= $lonW) AND (lat != $lat1 AND lon != $lon1) AND city != '' ORDER BY st, city, lat, lon";
if(!$rs = mysql_query($query)) {
echo "<strong>There was an error selecting nearby ZIP Codes from the database.</strong>\n";
}
elseif(mysql_num_rows($rs) == 0) {
echo "No nearby ZIP Codes located within the distance specified. Please try a different distance.\n";
}
else {
//output all matches to screen
while($row = mysql_fetch_array($rs)) {
echo "'$row[city]''$row[state]' '$row[zip_code]' '$row[lat]' '$row[lon]'\n";
}
}
}
?>