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 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号