doudang8824 2016-11-29 11:36
浏览 73
已采纳

空间几何SQL - 两个完全相同的查询,另一个不工作

This is bizzarre I have a database that stores polygons and a query running that checks if a point exists within any polygon and retrieves them. However when the query is created as an sql string in my php code it returns nothing however if I type in manually the query - it runs perfectly!

#This works
SELECT * FROM locations 
WHERE type = 'polygon' AND locationable_type = 'Notification' AND 
ST_CONTAINS(locations.geoshape, GeomFromText('Point(25.276987 55.296249)') ) ;

#This doesnt work
SELECT * FROM locations 
WHERE type = 'polygon' AND locationable_type = 'Notification' AND  
ST_CONTAINS(locations.geoshape, GeomFromText('Point(‎25.276987 55.296249)') );

Heres how the sql i actually being generated:

public function get_by_coords($latitude, $longitude){

// this grabs all the notifications
$sql = sprintf("SELECT * FROM locations WHERE type = 'polygon' AND locationable_type = 'Notification' 
        AND ST_CONTAINS(locations.geoshape, GeomFromText('point(%s, %s)') )", ($latitude), ($longitude));

Where $latitude, $longitude are actually passed as strings from a GET variable. I tried to typecast but the result was:

$latitude = "25.276987";
(float)$latitude; // equals zero

Whats going on here? I'm using Codeigniter here.

UPDATE

I just did a var_dump and found something weird. If I var_dump the created SQL query it shows there are 6 more characters than if I var_dump the query directly typed in a string ie:

string(166) "SELECT * FROM locations  WHERE type = 'polygon' AND locationable_type = 'Notification' AND  ST_CONTAINS(locations.geoshape, Point('‎25.27116987','‎55.292216249'))"
string(160) "SELECT * FROM locations  WHERE type = 'polygon' AND locationable_type = 'Notification' AND  ST_CONTAINS(locations.geoshape, Point('25.27116987','55.292216249'))"

The first string is generated while the second was as is - its shows there are 6 extra characters in the first string - I have a weird feeling those are causing issues.. how do I go further here...

  • 写回答

2条回答 默认 最新

  • dso15221 2017-02-15 09:33
    关注

    Hey guys sorry for the late response. I managed to find out what the issue was. I did a simple strlen on the sql generated vs the sql manually typed in and found a discrepency in the length. There were some kind of hidden characters - so did a simple remove non printable characters and it worked like a charm.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献