dtxa49711 2014-07-16 12:34
浏览 43
已采纳

SQL Query在phpMyAdmin中工作,但在php页面中不起作用

I have a weird problem happening:

I have a php page that compiles and executes a MySQL query to my web database. The goal is to try and determine if a point is within a polygon, using a custom non-MBR spatial relation function.

The query returns a syntax error result 1064.

Here is the echoed query that displays on my page:

SET @point = 'POINT(-63.610719000 44.669318000)'; SELECT * FROM `Zones` WHERE GISWithin(GeomFromText(@point), `zonePoly`) AND `zoneName` = 'trailerShareBoundary';

If I copy and paste that query string into phpMyAdmin, it works like a charm.

However, when the query originates from my php page, the following error is returned:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT * FROM `Zones` WHERE GISWithin(GeomFromText(@point), `zonePoly`) AND `zon' at line 1(1064)

And here is the php code that makes this all happen:

    if(!is_null($lng) && !is_null($lat)){

    //Create a mySQL variable to store a MySQL POINT object
    $query= "SET @point = 'POINT(".$lng." ".$lat.")'; ";

    //Test if the POINT variable is within the trailerShareBoundary variable using
    //custom MySQL function
    $query.= "SELECT * FROM `Zones` WHERE GISWithin(GeomFromText(@point), `zonePoly`) AND `zoneName` = 'trailerShareBoundary';";
    echo "<br/>".$query."<br/>";

    $result=mysqli_query($connection,$query);

    var_dump($result);
    echo '<br/>';

    if(!$result==false){
        $instructions = "<p>LOCK REQUEST VALID</p>";
    }else{
        $instructions = "<p>LOCK REQUEST INVALID</p>";
        echo mysqli_error($connection) . "(" . mysqli_errno($connection) . ")";
    }
    echo $instructions;

}

I've figured out a work-around:

If I first query the database to create the @point variable, then create a SEPARATE QUERY to query the database with my SELECT statement, it magically works! Here's what I mean:

This works:

    //Create a mySQL variable to store a MySQL POINT object
    $query= "SET @point = 'POINT(".$lng." ".$lat.")'; ";
    $result=mysqli_query($connection,$query);
    echo "<br/>".$query."<br/>";
    //Test if the POINT variable is within the trailerShareBoundary variable using
    //custom MySQL function
    $query= "SELECT * FROM `Zones` WHERE GISWithin(GeomFromText(@point), `zonePoly`) AND `zoneName` = 'trailerShareBoundary';";
    $result=mysqli_query($connection,$query);
    echo "<br/>".$query."<br/>";

So, can anyone tell me what's going on?

  • 写回答

2条回答 默认 最新

  • duanhuang1699 2014-07-16 12:37
    关注

    mysqli_query does not allow multiple queries, use mysqli_multi_query

    This should work:

    //Create a mySQL variable to store a MySQL POINT object
    $query = "SET @point = 'POINT(".$lng." ".$lat.")'; ";
    
    $query .= "SELECT * FROM `Zones` WHERE GISWithin(GeomFromText(@point), `zonePoly`) AND `zoneName` = 'trailerShareBoundary';";
    $result=mysqli_multi_query($connection,$query);
    echo "<br/>".$query."<br/>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化