参考具有变量的HAVING子句中的列别名

时间:2016-11-20 09:48:26

标签: php sql ajax

我正在尝试使用以下语句从数据库获取数据,但它不起作用。 这是我的php,由ajax调用以返回要在地图上显示的json数据 问题是如果HAVING子句将值作为变量,它不会返回任何内容。

<?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.\n';
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;
?>

0 个答案:

没有答案