Welcome Guest, Not a member yet? Register   Sign In
DB test connection details
#1

[eluser]Brad Martin[/eluser]
Building an installation function and i need to validate the database connection details that the user inputs.

I can't seem to find away to do this using codeigniters's database class.

have attempted the following but no success.
Code:
$config['hostname'] = $this->input->post('database_host');
$config['username'] = $this->input->post('username');
$config['password'] = $this->input->post('password');
$config['database'] = $this->input->post('database_name');
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = FALSE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";
if($this->load->database($config)){
        // details are correct
}else{
        // details are incorrect
}

After the connection details have been validated i use preg_replace(); to modify ./application/config/database.php to include the details.

I can get the result i want by using mysql_connect() however i would like to use codeigniter for this if possible.

I would think that when you call $this->load->database($config); that if for any reason it won't connect that codeigniter would return FALSE however that doesn't seem to be the case.
#2

[eluser]InsiteFX[/eluser]
You can try this, but not tested!
Code:
// Also your missing part of the database config! See default
$db['default']['hostname'] = "localhost";

if ( ! $this->load->database('default', TRUE))
{
    // Error connecting to database!
}
else
{
    // Connected!
}

InsiteFX
#3

[eluser]Brad Martin[/eluser]
Thanks this worked perfectly. The way i was doing it however is the way listed in the CI user guide:

http://ellislab.com/codeigniter/user-gui...cting.html

So maybe the guide needs to be updated to do it the way you suggested!
#4

[eluser]Brad Martin[/eluser]
After reviewing this again it actually didn't work.

Ive reviewed and tried the examples in the user guide but no luck.

With the above code if the details are correct or incorrect it still returns the DB object rather then TRUE or FALSE or some other way of determining if the connection was successful.

Any help would be greatly appreciated.
#5

[eluser]InsiteFX[/eluser]
You can try this, not tested and not sure if it will work!
Code:
$result = $this->db->conn_id;

InsiteFX
#6

[eluser]Brad Martin[/eluser]
Thanks that seemed to work needed to go back to the original way i did it but.
Code:
$config['hostname'] = $this->input->post('database_host');
$config['username'] = $this->input->post('username');
$config['password'] = $this->input->post('password');
$config['database'] = $this->input->post('database_name');
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = FALSE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";
$db_connection = @$this->load->database($config,TRUE);
if($db_connection->conn_id!=""){
// connected
}else{
// not connected
}




Theme © iAndrew 2016 - Forum software by © MyBB