Laravel complains about query with duplicate named parameters

后端 未结 2 1544
走了就别回头了
走了就别回头了 2020-12-20 09:54

When I do (in laravel):

 1,
]);

It says:

相关标签:
2条回答
  • 2020-12-20 10:26

    From what I can see it all comes down to mysql being unable to deal with named parameters.

    mysqli::prepare:

    This parameter can include one or more parameter markers in the SQL statement by embedding question mark (?) characters at the appropriate positions.

    pdo::prepare:

    You must include a unique parameter marker for each value you wish to pass in to the statement when you call PDOStatement::execute(). You cannot use a named parameter marker of the same name more than once in a prepared statement, unless emulation mode is on.

    Laravel has emulation mode disabled by default. One can enable it in config/database.php by adding 'options' => [PDO::ATTR_EMULATE_PREPARES => TRUE] to connection settings. That way you will get the same result as in pure php. Not sure that's a good idea, though.

    0 讨论(0)
  • 2020-12-20 10:39

    I usually solve that using a CROSS JOIN with a "constant" derived table (subquery in FROM clause). Then I can reuse the parameters as many times as I want.

    SELECT id
    FROM objects o
    CROSS JOIN (SELECT :lat as lat, :lng as lng) params
    WHERE ACOS(
        SIN(RADIANS(o.lat)) * SIN(RADIANS(params.lat))
        + COS(RADIANS(o.lat)) * COS(RADIANS(params.lat)) * COS(RADIANS(params.lng - o.lng))
    ) * 6371 < 10
    
    0 讨论(0)
提交回复
热议问题