I am trying to get data from database with the following statement,but it doesn't work. Here is my php that was called by ajax to return json data to be represented on map The problem is that it doesn't return anything if the HAVING clause take the value as variable.
<?php
$CustomerName=$_POST["Cust_Name"];
# Connect to MySQL database
$conn = new PDO('mysql:host=localhost;dbname=CustomersDB','root','admin');
# Build SQL SELECT statement including x and y columns
$sql = "SELECT *, _delivery_location_1_coordinates__latitude AS x, _delivery_location_1_coordinates__longitude AS y FROM customers HAVING nick_name_ = '".$CustomerName."'";
if (isset($_GET['bbox']) || isset($_POST['bbox'])) {
$bbox = explode(',', $_GET['bbox']);
$sql = $sql . ' WHERE x <= ' . $bbox[2] . ' AND x >= ' . $bbox[0] . ' AND y <= ' . $bbox[3] . ' AND y >= ' . $bbox[1];
}
$rs = $conn->query($sql);
if (!$rs) {
echo 'An SQL error occured.
';
exit;
}
$geojson = array(
'type' => 'FeatureCollection',
'features' => array()
);
while ($row = $rs->fetch(PDO::FETCH_ASSOC)) {
$properties = $row;
# Remove x and y fields from properties (optional)
unset($properties['x']);
unset($properties['y']);
$feature = array(
'type' => 'Feature',
'geometry' => array(
'type' => 'Point',
'coordinates' => array(
$row['x'],
$row['y']
)
),
'properties' => $properties
);
# Add feature arrays to feature collection array
array_push($geojson['features'], $feature);
}
header('Content-type: application/json');
echo json_encode($geojson, JSON_NUMERIC_CHECK);
$conn = NULL;
?>