【发布时间】:2012-10-14 05:10:09
【问题描述】:
我正在尝试从 ios 获取 lat 和 lon 坐标的输出(这工作正常),将其发送到 php 以使用 MySQL 进行查询,并让 php 将 xml 文档发送回 ios(此步骤不起作用因为它没有带回该位置内的 mysql 条目),然后在 iOS UItableview 上解析它(这也可以正常工作)。我试图让它与 XML 一起工作,因为我已经得到了一个更简单的 xml 脚本已经在它上面运行。但可能是由于缺乏 php 经验而导致的错误,我无法让这个 php 脚本工作!我在我的 php 脚本中做错了什么?谢谢!哦,还有,mysql 中的数据类别包括“lon”、“lat”和“name”(表示附近朋友或家人的名字)!如果有人想知道,这是早期脚本的演变版本(也产生相同的结果):php query for iOS latitude and longitude not searching for nearby mysql lat and lon with a xml output
<?php
define( 'LATMILES', 1 / 69 );
define( 'LONMILES', 1 / 53 );
if ( isset( $_GET['lat'] ) ) { $lat = (float)$_GET['lat']; } //Recieve ios input from: NSString *urlString = [NSString stringWithFormat:@"http://www.mysite.com/loc.php?lat=%g&lon=%g&radius=100&q=%@", latitude, longitude, searchBar.text?searchBar.text:@""];
if ( isset( $_GET['lon'] ) ) { $lon = (float)$_GET['lon']; } //Recieve ios input from: NSString *urlString = [NSString stringWithFormat:@"http://www.mysite.com/loc.php?lat=%g&lon=%g&radius=100&q=%@", latitude, longitude, searchBar.text?searchBar.text:@""];
if ( isset( $_GET['radius'] ) ) { $radius = (float)$_GET['radius']; } //Recieve ios input from: NSString *urlString = [NSString stringWithFormat:@"http://www.mysite.com/loc.php?lat=%g&lon=%g&radius=100&q=%@", latitude, longitude, searchBar.text?searchBar.text:@""];
$minlat = $lat - ( $radius * LATMILES );
$minlon = $lon - ( $radius * LONMILES );
$maxlat = $lat + ( $radius * LATMILES );
$maxlon = $lon + ( $radius * LONMILES );
$dbh = new PDO('(censored private information');
$sql = 'SELECT lat, lon, name FROM locations WHERE lat >= ? AND lat <= ? AND lon >= ? AND lon <= ?';
$params = array( $minlat, $maxlat, $minlon, $maxlon );
if ( isset( $_GET['q'] ) ) {
$sql .= " AND name LIKE ?";
$params []= '%'.$_GET['q'].'%';
}
$q = $dbh->prepare( $sql );
$q->execute( $params );
$doc = new DOMDocument();
$r = $doc->createElement( "locations" );
$doc->appendChild( $r );
foreach ( $q->fetchAll() as $row) {
$dlat = ( (float)$row['lat'] - $lat ) / LATMILES;
$dlon = ( (float)$row['lon'] - $lon ) / LONMILES;
$d = sqrt( ( $dlat * $dlat ) + ( $dlon * $dlon ) );
if ( $d <= $radius ) {
$e = $doc->createElement( "location" );
$e->setAttribute( 'lat', $row['lat'] );
$e->setAttribute( 'lon', $row['lon'] );
$e->setAttribute( 'name', $row['name'] );
$r->appendChild( $e );
}
}
print $doc->saveXML();
?>
【问题讨论】:
-
嗯...你读过我的问题了吗?这是一个脚本,由于脚本执行发生变化,需要重新发布...
-
系统不允许我撤消我的关闭投票。真可惜。如果您想避免被标记,请不要在您的问题中一遍又一遍地复制和粘贴相同的文本,并尝试将代码减少到相关的部分。否则,它看起来像是表面修改的同一个问题。遵循这些建议可能还有一个附带好处,那就是为您的问题吸引更多答案。
-
我会记住这些。另一方面,您对修复此脚本有什么建议吗?我应该去哪里看?
-
检查您的错误日志中的消息。使用调试器来单步调试代码,看看它在哪里做了你意想不到的事情。尤其要检查生成的 SQL 语句是否是您期望生成的。手动运行它,看看你是否得到了你期望的结果。您致电
execute(),但似乎没有检查错误结果。毫无疑问,所有 PDO 代码都应该包含在try/catch中。 -
谢谢!在 iOS 中,我什么也得不到。在浏览器中,我什么也得不到。还有其他替代程序可以用来检查我的 php 代码吗?
标签: php mysql ios xml location