Skip to content

Instantly share code, notes, and snippets.

@mr5z
Last active July 5, 2017 08:30
Show Gist options
  • Save mr5z/ee17407bbf9f623c1f91c00ef0501c55 to your computer and use it in GitHub Desktop.
Save mr5z/ee17407bbf9f623c1f91c00ef0501c55 to your computer and use it in GitHub Desktop.
get user by id
<?php
function getUserById($userId) {
$sql = "SELECT users.*,
COALESCE((SELECT locations.friendlyAddress
FROM locations
WHERE locations.userId = users.id
ORDER BY locations.dateGather
DESC LIMIT 1), 'Unavailable') lastKnownLocation,
COALESCE((SELECT CONCAT(locations.latitude, ',', locations.longitude)
FROM locations
WHERE locations.userId = users.id
ORDER BY locations.dateGather
DESC LIMIT 1), '') position
FROM users
WHERE users.id = $userId";
return query($sql);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment