gpt4 book ai didi

php - 无法使用 php 从 mysql 显示 XML 数据

转载 作者:行者123 更新时间:2023-11-29 14:47:38 27 4
gpt4 key购买 nike

我在关注 this 教程时发现,我无法查看预期的输出,因为我无法查看 xml 格式的数据。我稍微修改了一下配置数据,不知道是不是导致故障的原因?

<?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中了。

最佳答案

第一次通过问题:

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

尝试一下,让我们知道您看到的任何进一步的错误消息。

关于php - 无法使用 php 从 mysql 显示 XML 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6568211/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com