How to create a Webform pre-build option list via a query to an external database? I'm trying in Webform (version 4.9) to create a pre-build option list with data from an external database. A pre-build option list is an available list you get when creating a "select options" field in Webform (like list of countries, state, ...).

I didn't find an existing module that allows me to add such pre-build list to Webform.

So, first I added my external database information to the setting.php file.

To test it, I build a simple php page containing this: 

db_set_active('external_db');
$query = db_query("SELECT id, name FROM TABLE")-&gt;fetchAllKeyed();
db_set_active();
Print_r($query);


And, it's working (= I got a correct result of my request).

So then I added this to my .module file:

function webform_pre_build_list_db_webform_select_options_info() {
    $items = array();
    $items['modeles_db'] = array(
        'title' =&gt; t('Modeles_db'),
        'options callback' =&gt; 'webform_pre_build_list_db'
    );

   return $items;
}

function webform_pre_build_list_db() {

db_set_active('external_db');

return db_query('SELECT id, name FROM table')-&gt;fetchAllKeyed();

db_set_active('default');

}


And, it's not working (= my list is empty).

If I try with a request to the Drupal DB, then, it's working. For example:

function webform_pre_build_list_db() {
return db_query('SELECT cid, recipients FROM contact')-&gt;fetchAllKeyed();
}


Is working well, I got recipients form drupal table "contact" in my list...

What am I missing?

Any help will be appreciated!
I changed this:

function webform_pre_build_list_db() {
db_set_active('external_db');
return db_query('SELECT id, name FROM table')-&gt;fetchAllKeyed();
db_set_active('default');
}


to this

function webform_pre_build_list_db() {
  db_set_active('external_db');
  $options = db_query('SELECT id, name FROM table')-&gt;fetchAllKeyed();
  db_set_active('default');
  return $options;
}


And it's working!

(Explanation: return immediately ends the execution of a function, in this case before db_set_active() could be called.) 

Thanks to Nevets