我非常喜歡這種類型的東西,但我試圖自學有關PHP和MySQL數據庫。將舊代碼升級到更新的MySQL版本
我試圖建立的教訓我自己的本地副本就講到這兒:https://www.dougv.com/2009/03/27/getting-all-zip-codes-in-a-given-radius-from-a-known-point-zip-code-via-php-and-mysql/
我能得到它幾乎工作,但我認爲這是不正常的原因是因爲它被寫在2009年在一個現在過時的版本的MySQL。
任何人都可以請指出我正確的方向來更新此代碼?
<?php
if(isset($_POST['submit'])) {
if(!preg_match('/^[0-9]{5}$/', $_POST['zipcode'])) {
echo "<strong>You did not enter a properly formatted ZIP Code.</strong> Please try again.\n";
}
elseif(!preg_match('/^[0-9]{1,3}$/', $_POST['distance'])) {
echo "<strong>You did not enter a properly formatted distance.</strong> Please try again.\n";
}
else {
//connect to db server; select database
$link = mysql_connect('host_name', 'user_name', 'password') or die('Cannot connect to database server');
mysql_select_db('database_name') or die('Cannot select database');
//query for coordinates of provided ZIP Code
if(!$rs = mysql_query("SELECT * FROM php_zip_code_distance WHERE zip_code = '$_POST[zipcode]'")) {
echo "<strong>There was a database error attempting to retrieve your ZIP Code.</strong> Please try again.\n";
}
else {
if(mysql_num_rows($rs) == 0) {
echo "<strong>No database match for provided ZIP Code.</strong> Please enter a new ZIP Code.\n";
}
else {
//if found, set variables
$row = mysql_fetch_array($rs);
$lat1 = $row['latitude'];
$lon1 = $row['longitude'];
$d = $_POST['distance'];
//earth's radius in miles
$r = 3959;
//compute max and min latitudes/longitudes for search square
$latN = rad2deg(asin(sin(deg2rad($lat1)) * cos($d/$r) + cos(deg2rad($lat1)) * sin($d/$r) * cos(deg2rad(0))));
$latS = rad2deg(asin(sin(deg2rad($lat1)) * cos($d/$r) + cos(deg2rad($lat1)) * sin($d/$r) * cos(deg2rad(180))));
$lonE = rad2deg(deg2rad($lon1) + atan2(sin(deg2rad(90)) * sin($d/$r) * cos(deg2rad($lat1)), cos($d/$r) - sin(deg2rad($lat1)) * sin(deg2rad($latN))));
$lonW = rad2deg(deg2rad($lon1) + atan2(sin(deg2rad(270)) * sin($d/$r) * cos(deg2rad($lat1)), cos($d/$r) - sin(deg2rad($lat1)) * sin(deg2rad($latN))));
//display information about starting point
//provide max and min latitudes/longitudes
echo "<table class="\"bordered\"" cellspacing="\"0\"">\n";
echo "<tbody><tr><th>City</th><th>State</th><th>Lat</th><th>Lon</th><th>Max Lat (N)</th><th>Min Lat (S)</th><th>Max Lon (E)</th><th>Min Lon (W)</th></tr>\n";
echo "<tr><td>$row[city]</td><td>$row[state]</td><td>$lat1</td><td>$lon1</td><td>$latN</td><td>$latS</td><td>$lonE</td><td>$lonW</td></tr>\n";
echo "</tbody></table>\n\n";
//find all coordinates within the search square's area
//exclude the starting point and any empty city values
$query = "SELECT * FROM php_zip_code_distance WHERE (latitude <= $latN AND latitude >= $latS AND longitude <= $lonE AND longitude >= $lonW) AND (latitude != $lat1 AND longitude != $lon1) AND city != '' ORDER BY state, city, latitude, longitude";
if(!$rs = mysql_query($query)) {
echo "<strong>There was an error selecting nearby ZIP Codes from the database.</strong>\n";
}
elseif(mysql_num_rows($rs) == 0) {
echo "<strong>No nearby ZIP Codes located within the distance specified.</strong> Please try a different distance.\n";
}
else {
//output all matches to screen
echo "<table class="\"bordered\"" cellspacing="\"0\"">\n";
echo "<tbody><tr><th>City</th><th>State</th><th>ZIP Code</th><th>Latitude</th><th>Longitude</th><th>Miles, Point A To B</th></tr>\n";
while($row = mysql_fetch_array($rs)) {
echo "<tr><td>$row[city]</td><td>$row[state]</td><td>$row[zip_code]</td><td>$row[latitude]</td><td>$row[longitude]</td><td>";
echo acos(sin(deg2rad($lat1)) * sin(deg2rad($row['latitude'])) + cos(deg2rad($lat1)) * cos(deg2rad($row['latitude'])) * cos(deg2rad($row['longitude']) - deg2rad($lon1))) * $r;
echo "</td></tr>\n";
}
echo "</tbody></table>\n\n";
}
}
}
}
}
?>
非常感謝!
編輯:我應該提到,我更改了「從數據庫中選擇附近的郵政編碼時發生錯誤。」消息echo "<p>" . mysql_error() . " | $query</p>\n";
,我得到以下錯誤:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND longitude !=) AND city != '' ORDER BY state, city, latitude, longitude' at line 1 | SELECT * FROM zipcodedistance WHERE (latitude <= 0.144722858078 AND latitude >= -0.144722858078 AND longitude <= 0.144722858078 AND longitude >= -0.144722858078) AND (latitude != AND longitude !=) AND city != '' ORDER BY state, city, latitude, longitude
你得到了什麼特定的錯誤? – Jason 2014-08-29 01:28:43
將'mysql_'的所有實例更改爲'mysqli_',然後更改您的數據庫連接。 ['mysqli_''](http://php.net/manual/en/book.mysqli.php)的工作方式不同;它需要數據庫連接功能;查詢等,它首先。使用[**準備好的語句**](http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php)或[** PDO with prepared statements **](http:/ /php.net/pdo.prepared-statements),它更安全。 – 2014-08-29 01:33:05
@Jason謝謝,給OP添加了錯誤。 – Ads 2014-08-29 01:47:46