In tagging my NextGen images, I'm trying to show the images on that Tag's archive page.

Code:
$mf_query = <<<EOF
    SELECT t.*, tt.*, n.*
    FROM {$wpdb->term_relationships} as tr, {$wpdb->terms} AS t, {$wpdb->term_taxonomy} AS tt, {$wpdb->ngg_pictures} AS n
    WHERE tt.`taxonomy` = 'ngg_tag'
    AND n.`pid` = tr.`object_id`
    AND tr.`term_taxonomy_id` = tt.`term_taxonomy_id`
      AND tt.`term_id` = t.`term_id`
    ORDER BY rand()
    LIMIT 8
EOF;

$mf_terms = $wpdb->get_results( $mf_query );


    echo '<div class="gameCenter">        
    <table width="100%" border="2" cellpadding="5">
   <thead><tr><th>
     Game Center
    </th></tr></thead>
   <tfoot><tr><td>
    .
     </td></tr></tfoot>
   <tbody>
   <tr>
    <td>
    <div id="gameCenter">';


if( !empty( $mf_terms ) ) {
    
    foreach ( $mf_terms as $image  ) {
    
        echo '<a href="' . $image->imageURL . '" title="' . $image->pid . '"' .  $image->thumbcode . '>';
        
        echo '<img title="' . $image->alttext . '" alt="' . $image->alttext . '" src="' . $image->thumbnailURL . '"' . $image->size . '/>';
                
        }        
                        
    } 

    echo '</div></div>';