Jump to content

Store Locator with PHP and MySQL


jcbrmn06

Recommended Posts

MySQL server version is 5.1.44 and i downloaded XAMPP package for Mac OS X.  I am trying to create a store locator for a client of mine. I found this http://code.google.com/apis/maps/articles/phpsqlsearch.html on google. I went into this knowing nothing about MySQL and PHP, over the past few days i have been working on this and trying to get it to work. I have managed to get the map to come up and even connect to the database that i want to. Problem is when i go to search i keep coming up with "No results found". I guess my question is, what do you exactly have to do with the database? right now i exported it as an SQL and have it in the same folder as my .php file. Any ideas on how i can get this to work?? Any help would be greatly appreciated considering i know very very little about MySQL and PHP. Thanks.

Link to comment
https://forums.phpfreaks.com/topic/209036-store-locator-with-php-and-mysql/
Share on other sites

well i got this far:

Picture2.png

 

It seems like it is not picking up the database that i want it to. So i have a feeling that i may have put it in the wrong place or something. Also looking over that google article it wants to "Outputting XML with PHP" which i am guessing how i am querying it. This code below is what the article tells me to output it to XML. in the article below this code it wants me to test it.

 

"Call this PHP script from the browser to make sure it's producing valid XML."
I do not really know what the article means by calling this PHP script from the browser... :confused:

 

 

<?php  
require("phpsqlsearch_dbinfo.php");

// Get parameters from URL
$center_lat = $_GET["lat"];
$center_lng = $_GET["lng"];
$radius = $_GET["radius"];

// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);

// Opens a connection to a mySQL server
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
  die("Not connected : " . mysql_error());
}

// Set the active mySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die ("Can\'t use db : " . mysql_error());
}

// Search the rows in the markers table
$query = sprintf("SELECT address, name, lat, lng, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers 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);

$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)){
  $node = $dom->createElement("marker");
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("name", $row['name']);
  $newnode->setAttribute("address", $row['address']);
  $newnode->setAttribute("lat", $row['lat']);
  $newnode->setAttribute("lng", $row['lng']);
  $newnode->setAttribute("distance", $row['distance']);
}

echo $dom->saveXML();
?>

 

 

Hope this helps a little bit more.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.