2014-08-29 132 views
0

我非常喜欢这种类型的东西,但我试图自学有关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

+3

你得到了什么特定的错误? – Jason 2014-08-29 01:28:43

+2

将'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

+0

@Jason谢谢,给OP添加了错误。 – Ads 2014-08-29 01:47:46

回答

0

@Fred -ii-是正确的。所有mysql_ *都需要更改为mysqli_ *。

除此之外,您可能会遇到此SQL陈述的问题。

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 

如果$lat1$lon1是空的您的SQL将会失败。尝试将其更改为:

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` 

(我喜欢来包装'我的字段名`)

这样,如果$lat1$lon1是空的,你不会

AND (latitude != AND longitude !=) 
结束

你会以

AND (`latitude` != '' AND `longitude` != '')