Page 1 of 1

Passing Parameters using PHP

Posted: Thu Sep 29, 2011 6:33 pm
by gujunaid23
I'm trying to pass query parameters via the URL to the PHP. Below is the link:
http://www.icliqz.com/Dev/phpsqlsearch_ ... &radius=20

I was expecting xml of the results after passing the parameters, but I'm not recieving any results. I'm not sure what I'm doing wrong. :banghead:

I checked the SQL that is retrieving the results, and it's working:
SELECT user, latitude, longitude, ( 3959 * acos( cos( radians( 41.7636111 ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( -72.6855556 ) ) + sin( radians( 41.7636111 ) ) * sin( radians( latitude ) ) ) ) AS distance FROM tbl_UserLatLng HAVING distance <20 ORDER BY distance LIMIT 0 , 20

Below is the PHP code I'm using:

Code: Select all

// Select all the rows in the markers table
$query = sprintf("SELECT user, latitude, longitude, ( 3959 * acos( cos( radians( '%s') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( '%s' ) ) + sin( radians( '%s' ) ) * sin( radians( latitude ) ) ) ) AS distance
FROM tbl_UserLatLng
HAVING distance <'%s'
ORDER BY distance
LIMIT 0 , 20", 
  mysql_real_escape_string($center_lat),
  mysql_real_escape_string($center_lng),
  mysql_real_escape_string($center_lat),
  mysql_real_escape_string($radius));
$result = mysql_query($query);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}

header("Content-type: text/xml");

// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
  // ADD TO XML DOCUMENT NODE
  $node = $doc->createElement("marker");
  $newnode = $parnode->appendChild($node);

  $newnode->setAttribute("user", $row['user']);
  $newnode->setAttribute("latitude", $row['latitude']);
  $newnode->setAttribute("longitude", $row['longitude']);
  $newnode->setAttribute("distance", $row['distance']);
}

echo $doc->saveXML();
I'm not sure what I'm doing wrong.

Re: Passing Parameters using PHP

Posted: Thu Sep 29, 2011 8:36 pm
by Celauran
Have you tried echoing the query to make sure it matches what you've entered manually when testing? What about mysql_num_rows($result)? It will return FALSE if there's an error, but not if the query returns 0 rows.

Re: Passing Parameters using PHP

Posted: Thu Sep 29, 2011 9:14 pm
by gujunaid23
I believe I just figured it out. I told ya I was a PHP noob.
I didn't have the following code:

Code: Select all

$center_lat = $_GET["latitude"];
$center_lng = $_GET["longitude"];
$radius = $_GET["radius"];
Once I put this in, I was able to get the results I needed. Thanks!!!!!!

Re: Passing Parameters using PHP

Posted: Thu Sep 29, 2011 9:30 pm
by Celauran
Have you checked that $center_lat et al. contain values? I don't see them being assigned anywhere. I'm sure that bit was just omitted for brevity's sake, but when I recreated the code locally, the values are being properly passed into $query.

This is working fine on my server:

Code: Select all

// latitude=41.7636111&longitude=-72.6855556&latitude=41.7636111&radius=20

$latitude  = $_GET['latitude'];
$longitude = $_GET['longitude'];
$radius    = $_GET['radius'];

$query = sprintf("SELECT user, latitude, longitude, ( 3959 * acos( cos( radians( '%s') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( '%s' ) ) + sin( radians( '%s' ) ) * sin( radians( latitude ) ) ) ) AS distance
FROM tbl_UserLatLng
HAVING distance <'%s'
ORDER BY distance
LIMIT 0 , 20",
 mysql_real_escape_string($latitude),
 mysql_real_escape_string($longitude),
 mysql_real_escape_string($latitude),
 mysql_real_escape_string($radius)
);

echo $query;
Output:
[text]SELECT user, latitude, longitude, ( 3959 * acos( cos( radians( '41.7636111') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( '-72.6855556' ) ) + sin( radians( '41.7636111' ) ) * sin( radians( latitude ) ) ) ) AS distance FROM tbl_UserLatLng HAVING distance <'20' ORDER BY distance LIMIT 0 , 20 [/text]