Question

I created a customized module that offers to the user a simple interface to collect data about a new database: database name, user name, password... and then (after submitting the introduced values)connect the drupal site to the given database.how can I after that go to an another page that contains a list or a table that contains all the existing data tables?

 <?php 
 function testform_menu() {
 $items = array();

 $items['test'] = array(
 'title' => 'connect an external database',
 'page callback' => 'drupal_get_form',
 'page arguments' => array('testform'),
 'access callback' => TRUE,
 'type' => MENU_NORMAL_ITEM,
 );

 return $items;
 }

 function testform($form, &$form_state) {
 $form = array();
 $form['data'] = array(
 '#type' => 'fieldset',
 '#title' => t('Database parameters'),
  );
  $form['data']['databasename'] = array(
  '#type' => 'textfield',
  '#title' => t('enter the database name'),
  '#description' => t('a user interface to switch between databases'),
  '#size' => 28,
  '#required' => TRUE,

  );
   $form['data']['username'] = array(
   '#type' => 'textfield',
   '#title' => t('enter the data basebase user name'),
   '#description' => t('a user interface to switch between databases'),
   '#size' => 28,
   '#required' => TRUE,

   );
   $form['data']['userpass'] = array(
  '#type' => 'textfield',
  '#title' => t('enter the database password'),
  '#description' => t('a user interface to switch between databases'),
  '#size' => 28,
  '#required' => TRUE,

  );
  $form['data']['host'] = array(
  '#type' => 'textfield',
  '#title' => t('enter the database host name'),
  '#description' => t('a user interface to switch between databases'),
  '#size' => 28,
  '#required' => TRUE,

  );
   $form['data']['port'] = array(
  '#type' => 'textfield',
  '#title' => t('enter the database port name'),
  '#description' => t('a user interface to switch between databases'),
  '#size' => 28,
  '#required' => TRUE,

  );
   $form['data']['databasedriver'] = array(
  '#type' => 'textfield',
  '#title' => t('enter the database driver name'),
  '#description' => t('a user interface to switch between databases'),
  '#size' => 28,
  '#required' => TRUE,

  );

 $form['submit'] = array(
 '#type' => 'submit',
 '#value' => t('Submit'),
  );


  return $form;
}



 function testform_submit($form, &$form_state) {

 $database_name =$form_state['values']['databasename'];
 $user_name =$form_state['values']['username'];
 $user_pass =$form_state['values']['userpass'];
 $host =$form_state['values']['host'];
 $port =$form_state['values']['port'];
 $database_driver =$form_state['values']['databasedriver'];

 $db_array = array();
 $db_array['database'] = $database_name;
 $db_array['username'] = $user_name;
 $db_array['password'] = $user_pass; 
 $db_array['host'] = $host; //localhost
 $db_array['port'] = $port; //localhost
 $db_array['driver'] = $database_driver; //mysql

 try {
  Database::addConnectionInfo('OurDatabase', 'default', $db_array);

      db_set_active('OurDatabase'); 
  // Now actually do a check.
  $db1 = Database::getConnection();
      drupal_set_message('Database'.' ' .$database_name.' '. 'is now connected' );
   }
  catch (Exception $e) {    
    drupal_set_message(t('Failed to connect to your database server. The server reports the following message:
        %error.<ul><li>Is the database server running?</li><li>Does the database exist, and have you entered 
        the correct database name?</li><li>Have you entered the correct username and password?</li><li>Have you entered 
    the correct database hostname?</li></ul>', array('%error' => $e->getMessage())),'warning');
db_set_active();

 }

   db_set_active();

}
Was it helpful?

Solution

Assuming that you connect to a MySql database, you can do it simply by using something like

$result = Database::getConnection()->query("SHOW TABLES");

and then iterate, like

while ($table = $result->fetchAssoc())
    // Output here whatever you like
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top