简体   繁体   English

SQL查询可在phpMyAdmin中使用,但不适用于php页面

[英]SQL Query works in phpMyAdmin but not in php page

I have a weird problem happening: 我发生了一个奇怪的问题:

I have a php page that compiles and executes a MySQL query to my web database. 我有一个php页面,可编译并执行对我的Web数据库的MySQL查询。 The goal is to try and determine if a point is within a polygon, using a custom non-MBR spatial relation function. 目的是尝试使用自定义非MBR空间关系函数确定点是否在多边形内。

The query returns a syntax error result 1064. 该查询返回语法错误结果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. 如果我将查询字符串复制并粘贴到phpMyAdmin中,它的工作原理就像一个超级按钮。

However, when the query originates from my php page, the following error is returned: 但是,当查询源自我的php页面时,返回以下错误:

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: 这是使这一切发生的php代码:

    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! 如果我首先查询数据库以创建@point变量,然后创建一个SEPARATE QUERY来使用我的SELECT语句查询数据库,那么它神奇地起作用了! 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? 那么,谁能告诉我这是怎么回事?

mysqli_query does not allow multiple queries, use mysqli_multi_query mysqli_query不允许多个查询,请使用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/>";

In the first one you were appending the first query with second one. 在第一个查询中,您要在第一个查询后附加第二个查询。 And has also used semicolon in the query inside program . 并且在程序内部的查询中也使用了分号。 This is why first was showing error. 这就是为什么首先显示错误的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM