0

Quick question. I've got a SQL query which works with my database. However, I am to sure how to set parameters in my PHP file so that it works with my query?

Any Ideas? I'm having trouble with the Set parameters in terms of php and making my query work.

<?php

/*
 * Following code will list all the products
 */

// array for JSON response
$response = array();

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

set $orig_lat=32.42; 
set $orig_long=-23.550; 
set $bounding_distance=1;

// get all products from products table
$result = mysql_query(" SELECT * ,((ACOS(SIN($orig_lat * PI() / 180) * SIN(`latitude` * PI() /
180) + COS($orig_lat * PI() / 180) * COS(`latitude` * PI() / 180) *
COS(($orig_long - `longitude`) * PI() / 180)) * 180 / PI()) * 60 *
1.1515) AS `distance` FROM `Location` WHERE ( `latitude` BETWEEN
($orig_lat - $bounding_distance) AND ($orig_lat + $bounding_distance)
AND `longitude` BETWEEN ($orig_long - $bounding_distance) AND
($orig_long + $bounding_distance) ) ORDER BY `distance` ASC limit 1") or die(mysql_error()); 

// check for empty result
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // products node
    $response["Location"] = array();

    while ($row = mysql_fetch_array($result)) {
        // temp user array
       $Location = array();
       $Location["id"] = $row["id"];
       $Location["latitude"] = $row["latitude"];
       $Location["longitude"] = $row["longitude"];



        // push single product into final response array
        array_push($response["Location"], $Location);
    }
    // success
    $response["success"] = 1;

    // echoing JSON response
    echo json_encode($response);
} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "No Profiles found";

    // echo no users JSON
    echo json_encode($response);
}
?>
Ankhit Sharma
  • 367
  • 1
  • 5
  • 16
  • This question is a little short on information. Can you share what you have tried, and what problems you have run into? – Jay Blanchard May 08 '15 at 14:46
  • Typically, you would make a function. That function would contain arguments. At a minimum, those arguments might include $latitiude and $longitude (and, in this instance, $bounding). – Strawberry May 08 '15 at 14:50
  • @JayBlanchard See revised edits – Ankhit Sharma May 08 '15 at 14:52
  • 1
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's not as hard as you think](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 08 '15 at 14:54
  • What do you mean by "set parameters"? Are you getting errors? Setting parameters is just a matter of changing variable values. – Jay Blanchard May 08 '15 at 14:55
  • @JayBlanchard...Will start incorporating PDO. For now, the three Set $ variables I have, are not working with my query. Is that how its supposed to be written? It being referenced with an application and the app crashes, the php worked before because I had a simple query. I think the way I've written the statements are wrong is that the case? – Ankhit Sharma May 08 '15 at 14:58
  • @JayBlanchard, Nice article will work on it next time :) – Ankhit Sharma May 08 '15 at 15:01
  • Are you getting an error when you try to run the query? What is the error? – Jay Blanchard May 08 '15 at 15:03
  • 1
    @JayBlanchard, Yea it was giving me an error when I was running the application once it needed to get the reference from the file. However, It was just the case of removing the 'set' infront of the $orig values. It's fine now thank you! – Ankhit Sharma May 08 '15 at 15:07

1 Answers1

0

This was a simple syntax issue...The values that corresponded with the query was not noted well. In the PHP file you can't write set and declare variables using the $.

Original:

set $orig_lat=32.42; 
set $orig_long=-23.550; 
set $bounding_distance=1;

Solution:

$orig_lat=32.42; 
$orig_long=-23.550; 
$bounding_distance=1;
Ankhit Sharma
  • 367
  • 1
  • 5
  • 16