[英]PHP PDO Insert Longditude / latituded in Mysql and check if exists before insert
I am trying to insert geolocations correctly into my mysql db so that I may run a query to get all items within a set radius. 我试图将地理位置正确插入到我的mysql数据库中,以便我可以运行查询以获取设置半径内的所有项目。
my db is set up like so: --id (integer pk) --latlon (point spatial index) 我的数据库设置如下:--id(整数pk)--latlon(点空间索引)
and my function which checks if the geolocation exists and if not inserts looks like this: 我的检查地理位置是否存在以及是否插入的函数如下所示:
function addGeoLocation($long,$lat,$deal_id,$db)
{
if($long != '' && $long > 0){
$latlon = 'POINT('.$lat .' ' .$long.')';
$locs = $db->row("SELECT id FROM geolocations WHERE latlon = :latlon",array("latlon"=>$latlon));
if(!$locs)
{
$sql = $db->query("INSERT INTO geolocations set latlon = PointFromText(:latlon)",array("latlon"=>$latlon));
$location_id = $db->lastInsertId();
}
else
{
$location_id = $locs['id'];
}
$locs_pivot = $db->row("SELECT id FROM dailydeals_geolocations WHERE dailydeal_id = :deal_id AND geolocation_id = :location_id",array("deal_id"=>$deal_id,"location_id"=>$location_id));
if(!$locs_pivot)
{
$sql = $db->query("INSERT INTO dailydeals_geolocations (geolocation_id,dailydeal_id) VALUES(:location_id,:deal_id)",array("location_id"=>$location_id,"deal_id"=>$deal_id));
}
}
}
No matter what I try I just cannot get it to insert the geo I just keep getting this error: 无论我尝试什么,我都无法获取它来插入地理信息,而我一直不断收到此错误:
Unhandled Exception. <br />SQLSTATE[22003]: Numeric value out of range:
1416 Cannot get geometry object from data you send to the GEOMETRY field
Any help would be greatly received 任何帮助将不胜感激
Many thanks in advance :) 提前谢谢了 :)
This error of MySQL means that you are inserting a number value that is larger than the maximum allowed value for that column type. MySQL的此错误表示您要插入的数字值大于该列类型的最大允许值。
I think you need to check again what you are doing at " PointFromText " or you can change your column type to be larger enough to hold the given value. 我认为您需要再次检查“ PointFromText ”的操作,或者可以将列类型更改为足够大以容纳给定值。
我的代码实际上可以解决这个问题,因为我在locations表中也有一个latlon字段,我用lat lon值手动填充了该字段,因为此字段在导入时为null会引发错误:o)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.