尝试手动连接

时间:2013-04-23 19:37:03

标签: php codeigniter

我有一个带有字段的表单,用于使用CodeIgniter建立与数据库的连接。数据由表单中的用户填写。 是否可以知道连接是否已建立?

$drive      = $this->input->post('drive');
$hostname   = $this->input->post('hostname');
$database   = $this->input->post('database');
$username   = $this->input->post('username');
$password   = $this->input->post('password');
$base       = $this->input->post('base');

$db = array(
                'hostname' => $hostname,
                'username' => $username,
                'password' => $password,
                'database' => $database,
                'dbdriver' => $drive,
                'db_debug' => FALSE);

       $cn =  $this->load->database($db);

if ($cn) { echo 'ok'; } else {echo 'nops';} 

1 个答案:

答案 0 :(得分:2)

您明确禁用'db_debug' => false的错误消息,因此我不认为您会收到CI投诉。但是,您可以the same as the Database library does进行连接测试(因为每个字段都是公共的):

// your original code here
$conn_success = $cn->conn_id ? true : false;