我已经实现了我的数据库,然后我通过 PHP 从数据库中检索数据。
在我的代码中,我应该按名称过滤数据,如果名称存在,我想打印数据。尽管想要的项目在表中,但没有显示任何内容。我不明白错误在哪里。只有关于数据库中位置的信息,我没有将用户的位置保存在我的数据库中。
我的代码是:
<?php
$username="myusername";
$password="mypasswd";
$database="mydatabase";
$host="mysqlmyhost.com";
mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
// check for post data
if (isset($_GET["locationName"])) {
$locationName = $_GET['locationName'];
// get a product from products table
$result = mysql_query("SELECT *FROM Location WHERE locationName = $locationName");
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$product = array();
$product["locationName"] = $row["locationName"];
$product["locationInfo"] = $row["locationInfo"];
$product["locationLatitude"] = $row["locationLatitude"];
$product["locationLongitude"] = $row["locationLongitude"];
$product["locationPic"] = $row["locationPic"];
$product["city"] = $row["city"];
// success
$response["success"] = 1;
// user node
$response["Location"] = array();
array_push($response["Location"], $product);
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
编辑:
现在,我想向用户显示最近的位置。我已经搜索并更改了代码。我在以 $userLatitude=$_GET['userLatitude']; 开头的行出现错误 . 它说这里有一个意外的 t_string 错误。我无法理解错误是什么。你能再帮忙吗?
if (isset($_GET["userLatitude"]) && isset($_GET["userLongitude"])) {
$userLatitude=$_GET['userLatitude'];
$userLongitude=$_GET['userLongitude'];
$result = mysql_query("SELECT locationName, ( 6371 * acos( cos( radians( $userLatitude) ) * cos( radians( locationLatitude ) ) * cos( radians( locationLongitude ) - radians( $userLatitude) ) + sin( radians($userLongitude) ) * sin( radians( locationLatitude) ) ) ) AS distance
FROM Location HAVING distance < 2 ORDER BY distance LIMIT 0 ,20") or die(mysql_error());
echo $result;
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["Location"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array();
$product["locationName"] = $row["locationName"];
$product["locationInfo"] = $row["locationInfo"];
$product["locationLatitude"] = $row["locationLatitude"];
$product["locationLongitude"] = $row["locationLongitude"];
$product["locationPic"] = $row["locationPic"];
$product["city"] = $row["city"];
// push single product into final response array
array_push($response["Location"], $product);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No products found";
// echo no users JSON
echo json_encode($response);
}
mysql_close();