我创建了一个简单的用户表单,允许用户提供数据库名称,当用户提交时,drupal 站点将连接到给定的数据库名称,我在文件 settings.php:myDataBase 中定义了我的数据库,当我尝试
db_set_active('myDataBase');
它工作正常,但是当我在文本字段中键入 myDataBase 并提交然后使用提交的值连接到数据库时它不起作用。
<?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['Databasename'] = array(
'#type' => 'textfield',
'#title' => t('enter the database 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_validate($form, &$form_state) {
}
function testform_submit($form, &$form_state) {
if (db_set_active('myDataBase'))
{
drupal_set_message('database connected'); // works
}
// $database_name =$form_state['values']['Databasename']; // ius not working!!
// if ($database_name ) {
// db_set_active($database_name); // I even tried to parsing the submitted value to a string but didnt work in both case
// drupal_set_message(t('connected to the database'.' '.$database_name));
// }
db_set_active();
}