I have implemented my database and then I am retrieving data from database via PHP.
In my code I am supposed to filter data by name and if the name exists, I want to print the data. Although the wanted item is in the table, nothing is displayed. I could not understand where the error is. There is only information about the locations in the database , I am not keeping user's locations in my database.
My code is:
<?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);
}
?>
Edited :
Now,I would like to show the nearest locations to the user . I have searched and changed the code . I have an error on line beginning with $userLatitude=$_GET['userLatitude']; . It says there's an unexpected t_string error in here . I could not understand what the error is . Could you help again ?
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();