简体   繁体   中英

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.

my db is set up like so: --id (integer pk) --latlon (point spatial index)

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.

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.

我的代码实际上可以解决这个问题,因为我在locations表中也有一个latlon字段,我用lat lon值手动填充了该字段,因为此字段在导入时为null会引发错误:o)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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