How to find the user avatar filename in Drupal 7 In my custom module for Drupal 7 I've been using the following 2 SQL queries to find the user's avatar picture and the city (a custom text field):

function pref_block_view($block_name = '') {
  global $user;
  $viewer_id = $user-&gt;uid;

  if ($block_name == 'pref_main') {

    $result = db_query('select field_city_value from {field_data_field_city} where entity_id=:uid', array(':uid' =&gt; array($viewer_id)));
    $city   = $result-&gt;fetchField();

    $result = db_query('select filename from {file_managed} where uid=:uid', array(':uid' =&gt; array($viewer_id)));
    $avatar = $result-&gt;fetchField();


and at first this worked well (probably because each user hat just 1 picture - namely the avatar).

But then users attached pictures to blogs and books and now this query doesn't really work:

 # select * from drupal_file_managed where uid=1;
 fid  | uid |           filename           |                      uri                       |  filemime  | filesize | status | timestamp
------+-----+------------------------------+------------------------------------------------+------------+----------+--------+------------
  631 |   1 | 111.png                      | public://111_0.png                             | image/png  |   289191 |      1 | 1303376737
  843 |   1 | etud18.png                   | public://etud18.png                            | image/png  |   893838 |      1 | 1305905585
  527 |   1 | yaro-3.jpg                   | public://yaro-3.jpg                            | image/jpeg |    89814 |      1 | 1302367745
  529 |   1 | yaro-4.jpg                   | public://yaro-4_0.jpg                          | image/jpeg |   122129 |      1 | 1302367789
  638 |   1 | etud015.jpg                  | public://etud015_0.jpg                         | image/jpeg |   116956 |      1 | 1303459359
  835 |   1 | pref.jpg                     | public://pref_0.jpg                            | image/jpeg |    95079 |      1 | 1305838610


So I'm obviously using wrong query. Any help please?
Check the table "users", column "picture". Join that on the fid from the file_managed table.