使用php-xml-sql在谷歌地图上显示用户的问题

时间:2012-04-03 11:47:38

标签: php mysql xml google-maps

我正在使用google maps locator代码来获取某个半径范围内的用户(例如:25)。当我在运行后尝试将输出检查为xml文件时,它只显示预定义的echo语句而不是sql数据。我不知道sql是否检索数据或没有? 任何人都知道这个问题请回答。

这是我的PHP代码:

<?php   
  require("phpsqlajax_dbinfo.php");
  header("Content-type: text/xml");

  function parseToXML($htmlStr) 
  { 
    $xmlStr=str_replace('<','&lt;',$htmlStr); 
    $xmlStr=str_replace('>','&gt;',$xmlStr); 
    $xmlStr=str_replace('"','&quot;',$xmlStr); 
    $xmlStr=str_replace("'",'&#39;',$xmlStr); 
    $xmlStr=str_replace("&",'&amp;',$xmlStr); 
    return $xmlStr; 
  } 

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

  // 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());
  }

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

  // Start XML file, echo parent node
  echo "<markers>\n";
  // Iterate through the rows, printing XML nodes for each
  while ($row ==@mysql_fetch_assoc($result)){
    // ADD TO XML DOCUMENT NODE
    echo '<marker ';
    echo 'name="' . parseToXML($row['username']) . '" ';
    echo 'address="' . parseToXML($row['gender']) . '" ';
    echo 'lat="' . $row['lat'] . '" ';
    echo 'lng="' . $row['lng'] . '" ';
    echo 'distance="' . $row['distance'] . '" ';
    echo "/>\n";
  }

  // End XML file
  echo "</markers>\n";

?>

0 个答案:

没有答案