I'm trying to run the following raw query from Laravel:
DB::statement(
DB::raw("
SET @lon = ?;
SET @lat = ?;
"),
[$longitude, $latitude],
);
$places = DB::select(
DB::raw("
SELECT
id
FROM places
WHERE
position = POINT(@lon, @lat)
AND
language = ?
AND
LOWER(name) LIKE LOWER(CONCAT(?, '%'))
ORDER BY distance
LIMIT 10;
"),
[
$language,
$name,
],
);
The actual query is more complex but it's beyond the scope of the question. Suffice to say that I need @lon and @lat to be SQL variables.
This gives me the following error:
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 'SET @lat = ?' at line 2 (SQL: \n SET @lon = 7.4653;\n SET @lat = 51.5136;\n )
I had initially tried to put the SET statements inside the same string as the query, but some answers on stack overflow suggested they should be separated. Unfortunately that didn't work, but as far as I can see there isn't any actual error in the syntax, so what is going on?