簡體   English   中英

SQL查詢可在phpMyAdmin中使用,但不適用於php頁面

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

我發生了一個奇怪的問題:

我有一個php頁面,可編譯並執行對我的Web數據庫的MySQL查詢。 目的是嘗試使用自定義非MBR空間關系函數確定點是否在多邊形內。

該查詢返回語法錯誤結果1064。

這是顯示在我頁面上的回顯查詢:

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

如果我將查詢字符串復制並粘貼到phpMyAdmin中,它的工作原理就像一個超級按鈕。

但是,當查詢源自我的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)

這是使這一切發生的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;

}

我想出了一種解決方法:

如果我首先查詢數據庫以創建@point變量,然后創建一個SEPARATE QUERY來使用我的SELECT語句查詢數據庫,那么它神奇地起作用了! 這就是我的意思:

這有效:

    //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/>";

那么,誰能告訴我這是怎么回事?

mysqli_query不允許多個查詢,請使用mysqli_multi_query

這應該工作:

//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/>";

在第一個查詢中,您要在第一個查詢后附加第二個查詢。 並且在程序內部的查詢中也使用了分號。 這就是為什么首先顯示錯誤的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM