Php在mysql中使用st_contains进行查询

时间:2016-06-30 17:25:25

标签: php mysql stored-procedures

在我的项目中我想从mysql获取url。我在mysql中使用这个查询,它完美地提供了数据:

SELECT * FROM `data` WHERE ST_CONTAINS(data.area, Point(29.029087,40.990361))

但是我希望用php得到这个。 这是我的php:

<?php 

if($_SERVER['REQUEST_METHOD']=='GET'){

    $id  = $_GET['id'];

    require_once('dbConnect.php');



    $sql = "SELECT * FROM data WHERE ST_CONTAINS(data.area, Point(area='".$area."'))";

    $r = mysqli_query($con,$sql);

    $res = mysqli_fetch_array($r);

    $result = array();

    array_push($result,array(
        "stickUrl"=>$res['stickUrl']
        )
    );

    echo json_encode(array("result"=>$result));

    mysqli_close($con);

}

我希望在资源管理器中使用它来获取数据:

http://example.com/class/getData.php?area=29.029087,40.990361

但它说了这个错误:

  

警告:mysqli_fetch_array()期望参数1为mysqli_result,   第15行/vhosts/example.com/http/class/getData.php中给出的布尔值

{"result":[{"stickUrl":null}]}

但是当我用id改变这个查询并像这样搜索id时

$sql = "SELECT * FROM data WHERE id='".$id."'"; ;

http://example.com/class/getData.php?id=2

它完美运作。那我怎么解决这个问题呢?感谢

1 个答案:

答案 0 :(得分:0)

将您的查询检查为

  

mysqli_query失败时返回FALSE。

$sql = "SELECT * FROM data WHERE ST_CONTAINS(data.area, Point(area='".$area."'))";

// exho sql and copy/paste to test it in phpMyAdmin
echo $sql;

$r = mysqli_query($con,$sql);

if ($r) {
   $res = mysqli_fetch_array($r);    
   $result = array();    
   array_push($result,array(
      "stickUrl"=>$res['stickUrl']
      )
   );
   echo json_encode(array("result"=>$result));
} else {
   // Query Failed
}