Commit 2d1dce93 authored by michael lundquist's avatar michael lundquist
Browse files

Adding some error checking

parent be8254ef
<?php
include_once(dirname(__FILE__)."/queryPrep.php");
/*
thrown if the zipcode doesn't exist
*/
class ZipException extends Exception{
}
/*
returns the longitude and latitude of a zip code,
......@@ -12,13 +19,12 @@ function longLatZip($zip){
$result = resultQuery($sql, $param);
$retVal = False;//delete this line
if($result->num_rows > 0){
$retVal = $result->fetch_assoc();
$result->close();
//return here
}else{
//throw an error here
if($result->num_rows <= 0){
throw new ZipException("zipcode doesn't exist", 400);
}
$retVal = $result->fetch_assoc();
$result->close();
//return here
return $retVal;
}
......@@ -28,7 +34,6 @@ https://stackoverflow.com/questions/24370975/find-distance-between-two-points-us
*/
function nearbyZips($zip, $maxDistance){
$zipArr = longLatZip($zip);
if(!$zipArr) return false;
$sql = "SELECT P AS zipCode,
ROUND(
69.0 *
......@@ -51,6 +56,13 @@ function nearbyZips($zip, $maxDistance){
}
if(isset($_GET["zip"]) && isset($_GET["dist"])){
echo json_encode(nearbyZips($_GET['zip'], $_GET["dist"]));
try{
echo json_encode(nearbyZips(intval($_GET['zip']), intval($_GET["dist"])));
}catch(ZipException $e){
echo $e->getMessage();
echo json_encode(FALSE);
}
}else{
echo "params not passed right";
}
?>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment