Query, odd apostrophes seems to be generated leading to access violation

Hi I recently added an orderBy() to one of my queries but it seems that something is breaking the query.
Odd apostrophes appeared in the query leading to an access violation error.

I am calculating the distance from one point to another based on coordinates and sorting by the nearest.

Below is the result where 69 is 1 latitude degree. Notice the last 2 lines : an apostrophe appears after the comma before the 2 and another one appears after the closing parenthese. This makes no sense as the apostrophes vanishes when I remove the comma.

ORDER BY sqrt(
    pow(69 * (lat - originLat), 2) +
    pow(69 * (originLng) * cos(lat / (180/PI())), `2)
)` LIMIT 20

This orderBy is added by the following lines (currentLat and currentLng are the current latitude and longitude simplified so it does’nt bother the comprehension of the problem) :

$query->orderBy("
    sqrt(
        pow(69.1 * (event.latitude - currentLat), 2) +
        pow(69.1 * (currentLng - event.longitude) * COS(event.latitude / (180/PI())), 2)
    )
");

When I test this query in a sql console by removing the odd apostrophes it works perfectly.
Anyone as an idea ?

https://www.yiiframework.com/doc/api/2.0/yii-db-expression

1 Like

Thanks a lot, incredible speed answer :open_mouth: :smile: