php - Try Manual Connection

845

I have a form with fields to make a connection to the database using CodeIgniter. The data is filled in by the users in the form. Is it possible to know if the connection is established or not?

$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';} 
88

Answer

Solution:

You are explicitly disabling error messages with the'db_debug' => false, so I don't think you will get CI complaints. However you can do the same as the Database library does for connection testing (since every field is public):

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

People are also looking for solutions to the problem: Securing PHP sessions via hash which incorporates user-agent no longer works for IE10

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.