【发布时间】:2020-07-30 16:49:51
【问题描述】:
我希望在查询中执行自定义 MySQL 函数,以便更新(或插入)数据库中的值。
我的实际代码(带有自定义查询):
DB::statement('INSERT INTO locations(longitude, latitude, altitude, speed, point, user_id, circle)
VALUES (' . $long . ', ' . $lat . ', ' . $alt . ', ' . $speed . ', ' . $point . ', GETPOLYGON(' . $lat . ', ' . $long . ' , 0.300, 12))
ON DUPLICATE KEY UPDATE longitude=' . $long . ',latitude=' . $lat . ',altitude=' . $alt . ',speed=' . $speed . ',point=' . $point . ',circle=GETPOLYGON(' . $lat . ', ' . $long . ' , 0.300, 12)');
我真的不知道这样执行是否是我的最佳方式。
我的自定义函数是“getpolygon”。但这似乎不起作用
[2020-04-17 08:23:01] local.ERROR: PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 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 '43.325178, GETPOLYGON(43.325178, 1.121354 , 0.300, 12))
我在 SQL 中的函数:
CREATE FUNCTION getpolygon(lat DOUBLE, lon DOUBLE, radius SMALLINT, corner TINYINT) RETURNS geometry DETERMINISTIC BEGIN DECLARE i TINYINT DEFAULT 1; DECLARE a, b, c DOUBLE; DECLARE res TEXT; IF corner < 3 || radius > 500 THEN RETURN NULL; END IF; SET res = CONCAT(lat + radius / 111.12, ' ', lon, ','); WHILE i < corner do SET c = RADIANS(360 / corner * i); SET a = lat + COS(c) * radius / 111.12; SET b = lon + SIN(c) * radius / (COS(RADIANS(lat + COS(c) * radius / 111.12 / 111.12)) * 111.12); SET res = CONCAT(res, a, ' ', b, ','); SET i = i + 1; END WHILE; RETURN GEOMFROMTEXT(CONCAT('POLYGON((', res, lat + radius / 111.12, ' ', lon, '))')); END;
谢谢
【问题讨论】:
-
你没有给 user_id 值。问题可能出在哪里?
-
您将 radius 参数声明为 small int 并传递了一个浮点数...
-
另外你应该使用准备好的语句来避免sql注入漏洞
-
@Furkanöztürk 确实,这个问题(缺少 1 个参数)已解决,但它不能解决我的问题(谢谢!)
-
@EliasSoares 确实,我解决了这个问题(我的错)。如果我在 laravel 中使用 'DB::insert',我可以将我的 mysql 函数的结果作为列的值插入吗?