MySQL 8
WITH RECURSIVE `cte` (`level`, `id`, `login`, `referrer`) AS (
SELECT 0, `id`, `login`, `referrer`
FROM `users`
WHERE `id` = :userId
UNION
SELECT `u`.`level` + 1, `r`.`id`, `r`.`login`, `r`.`referrer`
FROM `cte` AS `u`
JOIN `users` AS `r` ON `r`.`id` = `u`.`referrer`
)
SELECT *
FROM `cte`