Can i write two foreach in only one? [PHP] : In my code i've this:

        $im  = $database->query("SELECT * FROM cms_messaggi_importanti WHERE messo_da = :id ORDER BY ID DESC", array("id"=>$functions->Utente("id")));
    
      
    foreach($im as $imp){
        
        $mex_i  = $database->query("SELECT * FROM cms_messaggi WHERE id = :id ORDER BY ID DESC", array("id"=>$imp['id_mex']));
    
      
    foreach($mex_i as $mex_imp){ 
    
    } }

Can i write this code in only one? Because i've to use a lot of variable with this method... is there a solution to my problem? For example.. using "JOIN".

Thanks!