【发布时间】:2014-08-21 15:34:52
【问题描述】:
我有以下两个查询,第一个从用户输入的前一页获取(发布的)邮政编码,并在邮政编码数据库中找到相应的纬度和经度。第二个查询应获取该数据并按距离对 100 英里内的任何内容进行排序。
它不工作,虽然网页加载正常,但我没有返回任何结果,即使我尝试在数据库中添加邮政编码。谁能看到我做错了什么?
$colname_LOCATION = "-1";
if (isset($_POST['ZIPCODE'])) {
$colname_LOCATION = $_POST['ZIPCODE']; //Zip code taken from customer form on previous page
}
mysql_select_db($database_XMASTREE, $XMASTREE); // find lat and lon from fields inZIP_FULL zipcode database
$query_LOCATION = sprintf("SELECT * FROM ZIP_FULL WHERE zip_code = %s", GetSQLValueString($colname_LOCATION, "int"));
$LOCATION = mysql_query($query_LOCATION, $XMASTREE) or die(mysql_error());
$row_LOCATION = mysql_fetch_assoc($LOCATION);
$totalRows_LOCATION = mysql_num_rows($LOCATION);
$lat = $row_LOCATION['latitude']; // code said to change $lat with a number so hopefully this will do it
$lon = $row_LOCATION['longitude']; // code said to change $lon with a number so hopefully this will do it
mysql_select_db($database_XMASTREE, $XMASTREE);
$query_DIST = "SELECT ((ACOS(SIN($lat * PI() / 180) * SIN(lat * PI() / 180) + COS($lat * PI() / 180) * COS(lat * PI() / 180) * COS(($lon - lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS distance
FROM MEMBERS
HAVING distance <= 100
ORDER BY distance ASC";
$DIST = mysql_query($query_DIST, $XMASTREE) or die(mysql_error());
$row_DIST = mysql_fetch_assoc($DIST);
$totalRows_DIST = mysql_num_rows($DIST);
【问题讨论】:
-
Please, don't use
mysql_*functions in new code。它们不再维护and are officially deprecated。看到red box?改为了解prepared statements,并使用PDO 或MySQLi - this article 将帮助您决定哪个。如果你选择 PDO,here is a good tutorial. -
谢谢,我会看那个。我使用的是 Dreamweaver CS6,它放入了 mysql_* 东西 - 仍在学习,这很有帮助,谢谢,确实会寻找更好的新方法。
-
不工作是什么意思?你得到一个sql错误吗?还是您收到页面错误?尝试
var_dump()your code 看看实际发生了什么。 -
您能否发布一些表格数据,以便我们尝试使用您的数据集样本解决此问题?