I need some help and clarification about how to do it all correctly, since it look like that I am doing it in the wrong and not most optimal way so it wasted my server resource.
Here is my code, which works, but I think it could be optimized for much faster performance if foreach is placed inside the while loop, instead of wrapping it like this :
$link_id = explode(',', $ref_links_id);
    foreach($link_id as $key => $value){
        $additional_links_query = 'SELECT * FROM additional_links WHERE id = '.$value.' ORDER BY ID asc';
        $res = $db->prepare($additional_links_query);
        $res->execute();
        while ($info = $res -> fetch()){
            $li_text = ($info['_ad_text'] == NULL)? '' : ' - '.$info['_ad_text'];
            $li_target = ($info['_ad_url_target'] == NULL)? '' : ' target="'.$info['_ad_url_target'].'"' ;
            $li_nofollow = ($info['_ad_nofollow'] == 1)? ' rel="nofollow"' : '';
            $li_cont = '<li><a href="'.$info['_ad_url'].'"'.$li_target.$li_nofollow.'>'.$info['_ad_anchor'].'</a>'.$li_text.'</li>';
        print<<<END
        $li_cont\n
        END;
    }
}
My initial approach was to place foreach loop inside the while loop, which could have save the round backs, but every attempt of mine has failed.