db_select based on userid: uid
If you want the logged in user info, try the following (I have not tested, but it should work).

global $user;

$hostname = db_query("SELECT hostname FROM {accesslog} WHERE uid = :uid LIMIT 1', array(':uid' =&gt;$user-&gt;uid))-&gt;fetchField();


The global $user object will return information from the currently logged in user.  You might want to put some authentication in to check that the user is logged in if this is used in an area where anonymous users can visit.  Maybe wrap it in an if statement like I've done below:

if (user_is_logged_in() == TRUE) {

  global $user;

  $hostname = db_query("SELECT hostname FROM {accesslog} WHERE uid = :uid LIMIT 1", array(':uid' =&gt; $user-&gt;uid))-&gt;fetchField();

  echo 'Hostname: ' . $hostname;
}
else {
  echo 'You are not logged in.';
}