I'm developing an application where it needs to show results based on the person longitude, latitude and user selected radius, then return the result in Json
array format json_encode();
I have 3 files in my project as sdbconnection.php, manipulate.php and pull.php. Below I have explained how exactly each file works.
sdbconnection.php is my server and database connection.
//MySQL Database Connection Credential
class connection {
public static $host = "XXXXX";
public static $user = "XXXXXXX";
public static $password = "XXXXXXX";
public static $database = "XXXXXXX";
public static $port = "XXXXX";
}
manipulate.php is how I exccute my SQL query and return the result. I have tried to figure out how to send requests from google tutorial, but i believe that I'm missing something since they are not using the PDO connection.
public function pullResults($longitude, $latitude, $radius){
$retunValue = array();
$sql = "SELECT title, latitude, longitude, ( 3959 * acos( cos( radians(:lat) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(:lng) ) + sin( radians(:lat) ) * sin( radians( latitude ) ) ) ) AS distance FROM posts HAVING distance < :radius ORDER BY distance LIMIT 0 , 20";
$result = $this->connection->query($sql);
if ($result != null && (mysqli_num_rows($result) >= 1)) {
$row = $result->fetch_array(MYSQLI_ASSOC);
if (!empty($row)) {
$retunValue = $row;
}
}
return $retunValue;
}
pull.php is where users request to execute their respond and when i try it in browser, I get Blank page.
//Database Connection and Query Manipulation.
require("handles/sdbconnection.php");
require("handles/manipulate.php");
// Get parameters from URL
$latitude = $_GET["latitude"];
$longitude= $_GET["longitude"];
$radius = $_GET["radius"];
$category = htmlentities($_GET["category"]);
//Connect to database
$databaseAccess = new manipulate();
$databaseAccess->openConnection();
//Get the Result
$databaseAccess->pullResults($longitude, $latitude, $radius);
//Close Connection After Data Pulled
$databaseAccess->closeConnection();
Also, if you see, I'm trying to be more specific in my MYSQL query by retrieving data by providing multiple categories ($category = htmlentities($_GET["category"]);
), I really appreciate if you show me how to do that as well.
Procedure: