Reputation: 301
this is my controller code for setting hostname,username,password,database_name programmatically & it's work properly in my code
function submit_installation()
{
$this->load->helper('url');
$this->load->helper('form');
$this->load->database();
if($_POST)
{
//--------------------to config database file----------------------
$old_hostname = '$db[\'default\'][\'hostname\']';
$old_username = '$db[\'default\'][\'username\']';
$old_password = '$db[\'default\'][\'password\']';
$old_database = '$db[\'default\'][\'database\']';
$fileName = "./application/config/database.php";
$file = file($fileName);
foreach( $file as $key=>$line ) {
if( false !== strpos($line, $old_hostname) )
{
$file[$key]=$old_hostname."='".$_POST['hostname']."';\n";
}
elseif( false !== strpos($line, $old_username) )
{
$file[$key]=$old_username."='".$_POST['username']."';\n";
}
elseif( false !== strpos($line, $old_password) )
{
$file[$key]=$old_password."='".$_POST['password']."';\n";
}
elseif( false !== strpos($line, $old_database) )
{
$file[$key]=$old_database."='".$_POST['database']."';\n";
}
}
file_put_contents($fileName, $file);
}
}
in database.php following code show database setting after execute controller file & changes made in database.php file.
<?php
$active_group = 'default';
$active_record = TRUE
$db['default']['hostname']='xxx';
$db['default']['username']='abc';
$db['default']['password']='abc';
$db['default']['database']='xyz';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
so how to check database connection is made or not using back-end code?
Upvotes: 1
Views: 186
Reputation: 217
I think You have to try this one:
$db_host = 'localhost';
$db_user = 'abc';
$db_pass = 'xyz';
$db_database = 'qwerty';
$link = mysql_connect($db_host,$db_user,$db_pass) or die('Unable to establish a DB connection');
This one is easy method.
Upvotes: 1