显示给定数据库名称中的所有表名称

时间:2013-11-11 13:41:59

标签: jquery drupal-forms

我创建了一个自定义模块,为用户提供了一个简单的界面来收集有关新数据库的数据:数据库名称,用户名,密码......然后(在提交引入的值之后)将drupal站点连接到给定的database.how之后我可以转到另一个包含列表或包含所有现有数据表的表的页面吗?

 <?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();

}

1 个答案:

答案 0 :(得分:0)

假设您连接到MySql数据库,您可以通过使用类似

的方式来实现
$result = Database::getConnection()->query("SHOW TABLES");

然后迭代,就像

一样
while ($table = $result->fetchAssoc())
    // Output here whatever you like
}