[英]Spatial Geomety SQL - two exact same queries one works the other doesn't
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! 但是,当查询在我的php代码中创建为sql字符串时,它什么也不会返回,但是如果我手动输入查询-它会完美运行!
#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: 这是我实际生成的sql的方式:
public function get_by_coords($latitude, $longitude){ 公共函数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. 实际上$latitude, $longitude
其中$latitude, $longitude
是作为字符串从GET变量传递的。 I tried to typecast but the result was: 我尝试过打字,但结果是:
$latitude = "25.276987";
(float)$latitude; // equals zero
Whats going on here? 这里发生了什么? I'm using Codeigniter here. 我在这里使用Codeigniter。
UPDATE 更新I just did a var_dump and found something weird. 我只是做了一个var_dump,发现了一些奇怪的东西。 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: 如果我使用var_dump创建的SQL查询,则显示的字符数比使用var_dump直接输入字符串的查询多6个字符,即:
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... 第一个字符串是生成的,而第二个字符串是原样生成的-它显示了第一个字符串中还有6个额外的字符-我有一种奇怪的感觉,这些都是引起问题的原因..我该怎么做...
As far as I remember in php functions we'll still need the dollar sign in front of the arguments. 据我记得,在php函数中,我们仍然需要在参数前面加上美元符号。
as you do ....($latitude), ($longitude));
就像....($latitude), ($longitude));
within the function 在功能内
thus your function arguments should be 因此你的函数参数应该是
public function get_by_coords($latitude,$longitude){
$sql = sprintf("SELECT * FROM locations WHERE type = 'polygon' AND locationable_type = 'Notification'
AND ST_CONTAINS(locations.geoshape, GeomFromText('point(%s, %s)') )", ($latitude), ($longitude));
}
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. 我对生成的sql和手动输入的sql进行了简单的查找,发现长度不符。 There were some kind of hidden characters - so did a simple remove non printable characters and it worked like a charm. 有一些隐藏的字符-简单地删除了不可打印的字符,它就像一种魅力。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.