好的,所以我被要求创建一个邮政编码搜索脚本。对于更高级的php / mysql,我还是比较陌生的。我所需要的只是帮助简化这个脚本。邮政编码数据库是整个英国邮政编码sql数据库1.3m行。因此,当我使用脚本获取半径内的所有邮政编码时,需要很长时间。有没有办法简化它。可能甚至使用Java / Ajax?
<?php
$postcode = str_replace( '+', '%20', $postcode );
$postcode = str_replace( '!"£$^&*()?<>', '', $postcode );
include ('config.php');
$sqlstring = "SELECT * FROM postcodelatlng WHERE postcode LIKE '".$postcode." %' ";
$result = mysql_query($sqlstring);
$row = mysql_fetch_assoc($result);
$lng = $row["longitude"] / 180 * M_PI;
$lat = $row["latitude"] / 180 * M_PI;
mysql_free_result($result);
$sqlstring2 = "SELECT DISTINCT
postcodelatlng.postcode,
(
6367.41 * SQRT(
2 * (
1- COS(RADIANS(postcodelatlng.latitude)) * COS(".$lat.") * (
SIN(RADIANS(postcodelatlng.longitude)) * SIN(".$lng.") + COS(RADIANS(postcodelatlng.longitude)) * COS(".$lng.")
) - SIN(RADIANS(postcodelatlng.latitude)) * SIN(".$lat.")
)
)
) AS Distance
FROM
postcodelatlng AS postcodelatlng
HAVING Distance <= '".$radius."'
ORDER BY Distance ";
$result1 = mysql_query($sqlstring2) or die('query failed: ' . mysql_error());
while($row = mysql_fetch_array($result1)){
$searchlets = "SELECT * FROM property_details WHERE postcode = '".$row['postcode']."' ";
$getresult = mysql_query($searchlets);
while($row2 = mysql_fetch_array($getresult)) {
echo ' Output here if Just Postcode was used ';
echo '<hr/>';
}
}
// This seraches if only city name was used
$searchlets2 = "SELECT * FROM property_details WHERE street = '".$postcode."' ";
$getresult2 = mysql_query($searchlets2);
while($row3 = mysql_fetch_array($getresult2)) {
echo ' Output Here If just city name was searched ';
echo '<hr/>';
}
mysql_close($con);
?>
答案 0 :(得分:2)
您可以尝试使用边界框来过滤掉结果。然后你可以使用harvesine配方来提高准确性。或者你可以尝试一个四核,a.k.a空间填充曲线。基本上它是一个空间索引并减少了维度。这是一个LSH算法。请在此问题中阅读我的回答:Geo-Search (Distance) in PHP/MySQL (Performance)