【问题标题】:Laravel MySql Syntax error or access violationLaravel MySql 语法错误或访问冲突
【发布时间】:2019-02-10 23:31:57
【问题描述】:
public static function rsine($coordinates)
{
    return '(6371 * acos(cos(radians(' . $coordinates['latitude'] . ')) 
    * cos(radians(`lat`)) 
    * cos(radians(`lng`) 
    - radians(' . $coordinates['longitude'] . ')) 
    + sin(radians(' . $coordinates['latitude'] . ')) 
    * sin(radians(`lat`))))';
}

输出:

"message": "SQLSTATE[42000]: 语法错误或访问冲突:1064 您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,了解在 '* 附近使用的正确语法,( 6371 * acos(cos(radians(28.392200)) \n * cos(radians(lat)) \n * cos(ra' at line 1 (SQL: select * from users where exists (select *, *, (6371 * acos(cos(弧度(28.392200)) \n * cos(弧度(lat)) \n * cos(弧度(lng) \n - 弧度(77.320801)) \n + sin(弧度( 28.392200)) \n * sin(radians(lat)))) AS 距离locations 其中users.location_id = locations.id and (6371 * acos(cos(radians(28.392200) )) \n * cos(弧度(lat)) \n * cos(弧度(lng) \n - 弧度(77.320801)) \n + sin(弧度(28.392200)) \n * sin(弧度( lat)))) distance asc) and users.deleted_at is null)",

【问题讨论】:

  • 你如何使用rsine()
  • $coordinates = ['latitude' => '28.392200', 'longitude' => '77.320801']; $rsine = Location::rsine($coordinates);

标签: php mysql laravel


【解决方案1】:

首先,如果你在这个平台上寻求帮助,下次尝试制定更像是一个问题。

其次,直接在查询中集成变量从来都不是一个好主意。这将使您的查询容易受到 sql 注入的影响。基础知识可以在here找到。

通过查看您的查询(在错误中),我们可以看到您将使用 * 通配符获取所有列两次。

select
    *
from
    users
where
    exists (
        select
            *,
            *, <-- This will throw a syntax error.
            (6371 * acos(co ... the rest of the distance calculation

【讨论】:

    【解决方案2】:

    最好将您的方法命名为 scopeRsine()。 但是不要为此使用mysql。改用 php。在这里查看我的回复:https://stackoverflow.com/a/50040011/1262144

    【讨论】:

      猜你喜欢
      • 2017-04-16
      • 2020-06-16
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-31
      • 2017-10-29
      • 2018-06-28
      • 2019-06-06
      相关资源
      最近更新 更多