我有一个表单,我可以填写我的数据库详细信息,然后将其放在database.php文件中。
问题
当我点击提交按钮时,它应加载我的迁移但无法加载迁移,因为说的数据库名称不正确。
原因是因为它与数据库同时加载并且还没有选择名称。
一旦建立了数据库连接或数据库名称,是否可以运行loadMigrations函数。
$this->form_validation->set_rules('hostname', 'Hostname', 'required');
$this->form_validation->set_rules('username', 'Database Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|xss_clean');
$this->form_validation->set_rules('database', 'Database', 'required');
$this->form_validation->set_rules('driver', 'Database Driver');
$this->form_validation->set_rules('prefix', 'Database Prefix');
$this->load->library('form_validation');
if ($this->form_validation->run($this) == false) {
$this->load->view('template/step_3');
} else {
$data = array();
$data['hostname'] = $this->input->post('hostname');
$data['username'] = $this->input->post('username');
$data['password'] = $this->input->post('password');
$data['database'] = $this->input->post('database');
$data['driver'] = $this->input->post('driver');
$data['prefix'] = $this->input->post('prefix');
$data = $this->load->view('template/configuration/database', $data, true);
write_file(dirname(FCPATH) . '/admin/application/config/database.php', $data, 'r+');
write_file(dirname(FCPATH) . '/catalog/config/database.php', $data, 'r+');
write_file(FCPATH . '/application/config/database.php', $data, 'r+');
/*
|
| This migration does not work because will not pick up database name
| You all ways have to refresh page. Need away that can load migrations
| Once there is a database name in the database.php file.
|
*/
$this->load->dbutil();
if($this->dbutil->database_exists($this->input->post('database'))) {
$this->load->library('migration');
$this->migration->version(1);
redirect('step_4'); // connected, go on
} else {
redirect('step_3'); // failed, go back
}
}
答案 0 :(得分:0)
您可以检查数据库连接,如下所示:
$db = $this->load->database('default', true);
if($connected = $db->initialize())
{
redirect('step_4'); // connected, go on
}
else
{
redirect('step_3'); // failed, go back
}
另一种方式
// Returns boolean or mysql/mysqli object
function test_db_connection()
{
$hostname = $this->input->post('hostname');
$database = $this->input->post('database');
$username = $this->input->post('username');
$password = $this->input->post('password');
$driver = $this->input->post('driver');
if($driver == 'mysqli')
{
return function_exists('mysqli_connect') && @mysqli_connect($hostname, $username, $password, $database);
}
$link = @mysql_connect("$hostname", $username, $password);
return $link && mysql_select_db("$database", $link)
}
我正在使用的这个功能是正常的。如果不起作用,请再次检查您的代码。