查询仅响应一个标签

时间:2019-07-10 15:14:48

标签: php mysql rest http get

我有这个URL响应

http://localhost/../qry/radius.php?lat=48.366591&lng=10.899801&radius=50&tags=mexican,italian

使用给定的信息填充该查询

SELECT o.id, o.title, o.icon, o.description,
    (
        6371 * acos
        (
            cos(
                radians( :lat )
            )
            * cos(
                radians( lat)
            ) * cos(
                radians( lng) - radians( :lng )
            ) + sin(
                radians( :lat )
            ) * sin(
                radians( lat)
            )
            )
            ) AS distance
            FROM objects o, category_object co, category c
            WHERE o.id = co.id_object AND co.id_category = c.id AND (c.title=:title1 OR c.title=:title2) HAVING distance/1000 <= :radius
            ORDER BY distance/1000 ASC;

Php代码

 $stmt = $db->prepare($qry);

    $stmt->bindParam(":lat", $latitude);
    $stmt->bindParam(":lng", $longtitude);
    for($i=1; $i<sizeof($tags_list) + 1; $i++) {
        $input = $tags_list[$i-1];
        $stmt->bindParam(":title" . $i, $input);
    }        
    $stmt->bindParam(":radius", $radius);

    if($stmt->execute()){
        while ($result = $stmt->fetch(PDO::FETCH_OBJ)) {

            $subquery = "SELECT c.title AS title FROM category c, category_object co, objects o
                         WHERE c.id = co.id_category AND co.id_object = o.id AND o.id=:id";

            $substmt = $db->prepare($subquery);

            $substmt->bindParam(":id", $result->id);

            $substmt->execute();

            $tagsForJson = "";
            while ($subresult = $substmt->fetch(PDO::FETCH_ASSOC)) {
                $tagsForJson = $tagsForJson . "," . $subresult['title'];
            }
            $tagsForJson = trim($tagsForJson, ",");

            echo json_encode(array("type" => "FeatureCollection",
                "features" => array(
                    "type" => "Feature",
                    "geometry" => array("type" => "Point",
                        "coordinates" => [$latitude, $longtitude]),
                    "properties" => array(
                        "ID" => $result->id,
                        "icon" => $result->icon,
                        "tags" => [$tagsForJson],
                        "title" => $result->title,
                        "description" => $result->description))), JSON_UNESCAPED_SLASHES);
            echo "\n\n";
        }

    }

问题在于查询仅返回与最后一个标签相关的结果,它完全忽略了标签“墨西哥”。 我已经在phpmyadmin中测试了该查询,并且可以正常运行,这就是为什么我怀疑bindParam周围的代码是问题所在。

谢谢您的帮助!

0 个答案:

没有答案