Displaying fields from my content types in a module I am learning how to write modules at the moment. The following piece of code simply pulls out of the database some data and selects just the nid, title and created fields

function photo_gallery_contents($display){

$max_num = variable_get('photo_gallery_max', 3);

$query = db_select('node', 'n')
-&gt;fields('n', array('nid', 'title', 'created'))
-&gt;condition('status', 1) //Published.    
-&gt;condition('type','portfolio') // Only want our photos aka Portfolio content type
-&gt;orderBy('created', 'DESC'); //Most recent first.

if ($display == 'block') {
// Restrict the range if called with 'block' argument
$query-&gt;range(0, $max_num);
} // Now proceeds to execute().
// If called by pge, query proceeds directly to execute()

return $query-&gt;execute();


}

This works fine but how do I access the other fields I created for my "portfolio" content type such as "field_image" or "field_category" both of which I added? I just cannot work out how to do this. Your help will be so appreciated!

I've written other hooks that represent this information on the page or block, at the moment I am showing a list of links to my nodes. I want them to be images with links instead:

function _photo_gallery_page() {
$result = photo_gallery_contents('page');
//Array to contain items for the page to render.
$items = array();
//Iterate over the resultset and format as links.
foreach ($result as $node) {
  $items[] = array(
  'data' =&gt; l($node-&gt;title, 'node/' . $node-&gt;nid),
  ); 
}

if (empty($items)) { //No content in the last 2 weeks.
  $page_array['photo_gallery_arguments'] = array(
    //Title serves as page subtitle
    '#title' =&gt; t('All photos from ever'),
    '#markup' =&gt; t('No photos available.'),
  );
  return $page_array;  
} 

 else {
  $page_array['photo_gallery_arguments'] = array(
    '#title' =&gt; t('All photos from ever'),
    '#items' =&gt; $items,
    //Theme hook with suggestion.  
    '#theme' =&gt; 'item_list__photo_gallery',
  );
  return $page_array;
}


}

So how do I swap $node->title to show the image instead?
The best way would be to grab just the node ids from the table, and use the node_load_multiple() function to load the node objects. 

The field data will be attached to those objects, and you can use other API functions to extract the data/build the display HTML (field_get_items() and field_view_field() respectively).

Some sample code that should help:

function photo_gallery_contents($display){
  $max_num = variable_get('photo_gallery_max', 3);

  $query = db_select('node', 'n')
    -&gt;fields('n', array('nid'))
    -&gt;condition('status', 1) //Published.    
    -&gt;condition('type','portfolio') // Only want our photos aka Portfolio content type
    -&gt;orderBy('created', 'DESC'); //Most recent first.

  if ($display == 'block') {
    // Restrict the range if called with 'block' argument
    $query-&gt;range(0, $max_num);
  }

  // Fetch the results for the nid field into an array
  $nids = $query-&gt;execute()-&gt;fetchCol(); 

  // Load the node objects and return
  return node_load_multiple($nids);
}

function _photo_gallery_page() {
  $result = photo_gallery_contents('page');

  //Array to contain items for the page to render.
  $items = array();
  //Iterate over the resultset and format as links.
  foreach ($result as $node) {
    // Build a render array based on the field's display settings.
    $image = field_view_field('node', $node, 'field_image');

    // Render the array down to the image HTML
    $image_html = drupal_render($image);

    $items[] = array(
      'data' =&gt; l($image_html, 'node/' . $node-&gt;nid, array('html' =&gt; TRUE)),
    ); 
  }
}