无法使用php从mysql显示XML数据 [英] XML data can't be displayed from mysql using php

查看:105
本文介绍了无法使用php从mysql显示XML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在发现教程,我无法查看预期的输出,因为我无法查看xml格式的数据.我已经稍微更改了配置数据,不知道这是否是导致故障的原因?

I'm following this tutorial when I found out, I can't view the expected output because I can't view my data in xml format. I have change the configuration data a little bit, didn't know if it was the cause of malfunction?

<?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, 'root', '12345678');
if (!$connection) {
  die("Not connected : " . mysql_error());
}

// Set the active mySQL database
$db_selected = mysql_select_db('demo', $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);

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();
?>

我希望得到类似的结果,例如 http: //gmaps-samples.googlecode.com/svn/trunk/articles-phpsqlsearch/phpsqlsearch_expectedoutput.xml ,但是什么也没有出现.似乎它没有任何数据,但我已经将数据导入了我的mysql.

I am expecting to get a similar result like this http://gmaps-samples.googlecode.com/svn/trunk/articles-phpsqlsearch/phpsqlsearch_expectedoutput.xml but nothing appears. It seems like it doesn't have any data but I have already imported the data in my mysql.

推荐答案

问题的第一遍:

<?php  

// Get parameters from URL
$center_lat = ( isset( $_GET["lat"] ) ? $_GET["lat"] : 0 ); # You could replace these "0"s with the
$center_lng = ( isset( $_GET["lng"] ) ? $_GET["lng"] : 0 ); # Lat/Lng of a default location.
$radius     = ( isset( $_GET["radius"] ) ? $_GET["radius"] : 10 ); # Again, default 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
if( !( $connection = mysql_connect( 'localhost' , 'root' , '12345678' ) ) ) # The SQL Server address must be a string or IP address
  die( "MySQL Error - Failed to connect to Server : " . mysql_error() );

// Set the active mySQL database
if( !mysql_select_db( 'demo' , $connection ) );
  die( "MySQL Error - Failed to connect to Database : " . 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 );

if( !$result )
  die( "MySQL Error - Invalid query: " . mysql_error() . ' "'.$query.'"' ); # During debugging, echo the SQL Statement on Error

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

// Iterate through the rows, adding XML nodes for each
if( mysql_num_rows( $result ) ){
  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']);
  }
}else
  die( 'MySQL Error - No Records Returned "'.$query.'"' );

echo $dom->saveXML();
?>

试试看,让我们知道您看到的其他错误消息.

Give that a go, and let us know any further error messages you are seeing.

这篇关于无法使用php从mysql显示XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆