application/config/
database.php,
How can I set another database with same localhost,
Please help! thanks in Advance
$active_group = 'default';
$active_record = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'business_permit',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
The best way is to use different database groups. If you want to keep using the master database as usual ($this->db) just turn off persistent connection configuration option to your secondary database(s). Only master database should work with persistent connection :
Master database
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "database_name";
$db['default']['dbdriver'] = "mysql";
$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;
Secondary database (notice pconnect is set to false)
$db['otherdb']['hostname'] = "localhost";
$db['otherdb']['username'] = "root";
$db['otherdb']['password'] = "";
$db['otherdb']['database'] = "other_database_name";
$db['otherdb']['dbdriver'] = "mysql";
$db['otherdb']['dbprefix'] = "";
$db['otherdb']['pconnect'] = FALSE;
$db['otherdb']['db_debug'] = FALSE;
$db['otherdb']['cache_on'] = FALSE;
$db['otherdb']['cachedir'] = "";
$db['otherdb']['char_set'] = "utf8";
$db['otherdb']['dbcollat'] = "utf8_general_ci";
$db['otherdb']['swap_pre'] = "";
$db['otherdb']['autoinit'] = TRUE;
$db['otherdb']['stricton'] = FALSE;
Then you can use secondary databases as database objects while using master database as usual :
// use master dataabse
$users = $this->db->get('users');
// connect to secondary database
$otherdb = $this->load->database('otherdb', TRUE);
$stuff = $otherdb->get('struff');
$otherdb->insert_batch('users', $users->result_array());
// keep using master database as usual, for example insert stuff from other database
$this->db->insert_batch('stuff', $stuff->result_array());
Hopes answered your question.
Google first before you ask the question... ;)
Codeigniter - multiple database connections
you have use environment to use multiple database
create config folder inside production and development folder and add database.php file
then root path index.php file set environment
Related
This question already has an answer here:
Datebase error mysqli::real_connect(): (HY000/2002): Connection refused codeingiter
(1 answer)
Closed 2 years ago.
<?php
$active_group = 'default';
$query_builder = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'campus';
$db['default']['password'] = 'Mac#123';
$db['default']['database'] = 'campusnew';
$db['default']['dbdriver'] = 'mysqli';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$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'] = TRUE;
$db['otherdb']['hostname'] = "101.51.150.42";
$db['otherdb']['username'] = 'software';
$db['otherdb']['password'] = 'Lmsnew';
$db['otherdb']['database'] = 'lmsnew';
$db['otherdb']['dbdriver'] = "mysqli";
$db['otherdb']['dbprefix'] = "";
$db['otherdb']['pconnect'] = TRUE;
$db['otherdb']['db_debug'] = FALSE;
$db['otherdb']['cache_on'] = FALSE;
$db['otherdb']['cachedir'] = "";
$db['otherdb']['char_set'] = "utf8";
$db['otherdb']['dbcollat'] = "utf8_general_ci";
$db['otherdb']['swap_pre'] = "";
$db['otherdb']['autoinit'] = TRUE;
$db['otherdb']['stricton'] = TRUE;
model function
public function newusersignin()
{
$DB2 = $this->load->database('otherdb', TRUE);
$firstName=$this->input->post('firstName');
$lastName=$this->input->post('lastName');
$timezone=$this->input->post('timezone');
$fullname=$firstName.' '.$lastName;
$email=$this->input->post('email');
$phone=$this->input->post('phone');
$ip_address=$this->input->ip_address();
$institutional=$this->input->post('institutional');
$pass=$firstName.mt_rand(1,100);
$username=$email;
$password=$this->hash($pass);
$insertnewuser= array('name' =>$fullname,'email'=>$email,'phone'=>$phone,'username'=>$username,'password'=>$password,'usertype'=>'ClgAdmin','create_date'=>date('y-m-d H:i:s'),'create_userID'=>1,'create_username'=>'campus','systemadminactive'=>0,'timezone'=>$timezone,'status'=>1 );
$this->db->insert('admin',$insertnewuser);
$DB2->insert('admin',$insertnewuser);
}
In this question I am simply create two database connection one is for localhost and another one is IP which is hosted on different server. Now, what happen when I am going to insert form data in my IP Server then it throw an error and i.e.
Message: mysqli::real_connect(): (HY000/2002): Connection refused
I don't know why this happen? How can I solve this problem? Please help me.
Thank You
Instead of doubling everything in two arrays, can you check against the URL?
Just change the localhost to be more specific as to which db you need to connect too
$is_local = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}";
if (strpos($is_local, 'localhost') !== false) {
// LOCAL
$db_name = "db_name";
$db_user = "root";
$db_pass = "root_pass";
$db_host = "localhost";
} else {
// OTHER
$db_name = "db_name_2";
$db_user = "root_live";
$db_pass = "root_pass_live";
$db_host = "mysql.live.com";
}
Then your connection code is always the same from then
Database Configuration
CodeIgniter has a config file that lets you store your database connection values (username, password, database name, etc.). The config file is located at application/config/database.php. You can also set database connection values for specific environments by placing database.php in the respective environment config folder.
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'database_name',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => TRUE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array()
);
Inserting Data
$data = array(
'title' => 'My title',
'name' => 'My Name',
'date' => 'My date'
);
$this->db->insert('mytable', $data);
Your database configuration and methodology given above are correct. All you have to do is allow remote connection for your database on your server. If you are using CPanel, you can browse to "Remote MySQL" and add access host. Add your ISP IP address to allow only your computer or "%" for any computer to access.
I Have mad an installer for my website. but I don't how to replace hostname,username, password and database. with the values which are coming from input fields.
This is the code from config/database.php file.
$db['default'] = array(
'dsn' => '',
'hostname' => 'db_host',
'username' => 'db_user',
'password' => 'db_pass',
'database' => 'db_name',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
but this code gives me that error.
and this code in controller.
function configure_database() {
// write database.php
$data_db = file_get_contents('./application/config/database.php');
// session_start();
$data_db = str_replace('db_name', $_POST['dbname'], $data_db);
$data_db = str_replace('db_user', $_POST['username'], $data_db);
$data_db = str_replace('db_pass', $_POST['password'], $data_db);
$data_db = str_replace('db_host', $_POST['hostname'], $data_db);
file_put_contents('./application/config/database.php', $data_db);
}
You could format the config/database.php as follows :
$db['default']['username'] = "%USERNAME%";
$db['default']['password'] = "%PASSWORD%";
$db['default']['database'] = "%DATABASE%";
$db['default']['hostname'] = "%HOSTNAME%";
$db['default']['dbdriver'] = "mysqli";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
Then write to it using this method :
function configure_database() {
// write database.php
$data_db = file_get_contents('config/database.php');
// session_start();
$temporary = str_replace("%DATABASE%", $_POST['dbname'], $data_db);
$temporary = str_replace("%USERNAME%", $_POST['username'], $temporary);
$temporary = str_replace("%PASSWORD%", $_POST['password'], $temporary);
$temporary = str_replace("%HOSTNAME%", $_POST['hostname'], $temporary);
// Write the new database.php file
$output_path = './application/config/database.php';
$handle = fopen($output_path,'w+');
// Chmod the file, in case the user forgot
#chmod($output_path,0777);
// Verify file permissions
if(is_writable($output_path)) {
// Write the file
if(fwrite($handle,$temporary)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
Basically it checks on every line for a match and then replace them with the posted data, and lastly write it to the database config again.
Reference : CodeIgniter Installer
If you would like to dynamically set a config item or change an existing one, you can do so using:
$this->config->set_item('item_name', 'item_value');
Where item_name is
the $config array index you want to change, and item_value is its
value.
So you can try this:
// validate the input
if(isset($_POST['dbname']){
$this->config->set_item('hostname',$_POST['dbname']);
}
...
I have updated my codeigniter version from 2.2.4 step by step to 3.0.6 and I get an error:
An uncaught Exception was encountered
Type: Error
Message: Call to undefined function mysql_pconnect()
Filename: path-to-project\system\database\drivers\mysql\mysql_driver.php
Line Number: 135
Backtrace:
File: path-to-project\application\controllers\Main.php
Line: 10
Function: __construct
File: path-to-project\index.php
Line: 315
Function: require_once
I have just replaced my index.php file and system directory with the new one and made some changes in my application according to tutorial.
and this is the Main controller:
class Main extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('main_model');
}
}
What causes the problem?!
And this is the link of the tutorial.
Deprecated features in PHP 5.5.x:
The original MySQL extension is now deprecated, and will generate E_DEPRECATED errors when connecting to a database. Instead, use the MySQLi or PDO_MySQL extensions.
You're using the deprecated mysql dbdriver. Locate the config/database.php file and change dbdriver to use mysqli :
$db['default']['dbdriver'] = 'mysqli';
Thanks to Anant
I come to a conclusion:
I completely changed my old database.php file in config folder with the new one:
From:
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['database'] = '';
$db['default']['dbdriver'] = '';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$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;
To:
$db['default'] = array(
'dsn' => '',
'hostname' => '',
'username' => '',
'password' => '',
'database' => '',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
And the error is gone!
go to application/config/database.php
and just change mysql to mysqli
like
this was before:
$db['default']['dbdriver'] = 'mysql';
this was after solution:
$db['default']['dbdriver'] = 'mysqli';
i just change mysql to mysqli. that's it
my error was
Fatal error: Uncaught Error: Call to undefined function
mysql_pconnect() in
E:\manish_data\software\xampp\htdocs\ci2\system\database\drivers\mysql\mysql_driver.php:92
Stack trace: #0
E:\manish_data\software\xampp\htdocs\ci2\system\database\DB_driver.php(116):
CI_DB_mysql_driver->db_pconnect() #1
E:\manish_data\software\xampp\htdocs\ci2\system\database\DB.php(149):
CI_DB_driver->initialize() #2
E:\manish_data\software\xampp\htdocs\ci2\system\core\Loader.php(347):
DB(Array, NULL) #3
E:\manish_data\software\xampp\htdocs\ci2\application\models\usermodel.php(20):
CI_Loader->database() #4
E:\manish_data\software\xampp\htdocs\ci2\application\controllers\users.php(15): UserModel->getUsers() #5
E:\manish_data\software\xampp\htdocs\ci2\system\core\CodeIgniter.php(360):
Users->index() #6
E:\manish_data\software\xampp\htdocs\ci2\index.php(202):
require_once('E:\manish_data\...') #7 {main} thrown in
E:\manish_data\software\xampp\htdocs\ci2\system\database\drivers\mysql\mysql_driver.php
on line 92
If this error happened when you're hosting the website, make sure to set the correct PHP Version (the one your CI use).
I have seen and tried all the solutions in Codeigniter - multiple database connections, You have specified an invalid database connection group codeigniter error, firebird - codeigniter connection and none of them worked.
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'testing';
$db['default']['dbdriver'] = 'mysqli';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$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;
$db['fbird']['hostname'] = "localhost";
$db['fbird']['username'] = "SYSDBA";
$db['fbird']['password'] = "masterkey";
$db['fbird']['database'] = "C:\waitkyl.fdb";
$db['fbird']['dbdriver'] = "firebird";
After this configuration, I call the fbird database this way:
class Fb_model extends CI_Model{
public function __construct(){
parent::__construct();
}
public function getAll(){
$db = $this->load->database('fbird', TRUE);
return $db->get('categories')->result();
}
}
And the error I get after trying output the values print_r($this->fb_model->getAll()); is:
Fatal error: Call to a member function result() on a non-object
That usually means that the table 'categories' doesn't exists..which isn't true.
So I have tried to change my configuration file into:
$db['fbird']['hostname'] = "localhost";
$db['fbird']['username'] = "SYSDBA";
$db['fbird']['password'] = "masterkey";
$db['fbird']['database'] = "C:\waitkyl.fdb";
$db['fbird']['dbdriver'] = "firebird";
$db['fbird']['dbprefix'] = "";
$db['fbird']['pconnect'] = FALSE;
$db['fbird']['db_debug'] = TRUE;
$db['fbird']['cache_on'] = FALSE;
$db['fbird']['cachedir'] = "";
$db['fbird']['char_set'] = "utf8";
$db['fbird']['dbcollat'] = "utf8_general_ci";
And also removed from the php.ini file the ; from the following lines:
extension=php_pdo_firebird.dll
extension=php_interbase.dll
And if I refresh the page now the error I receive is:
Unable to connect to your database server using the provided settings.
Filename: C:\xampp\htdocs\projtesting\system\database\DB_driver.php
Line Number: 124
In order to see if the credentials from the database were fine I opened it on the software SQL Manager 2008 Lite for Interbase and Firebird, and it was ok.
Any ideas of what's going wrong?
Solution: Codeigniter 3 (dbdriver = ibase)
$db['firebird'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'sysdba',
'password' => 'masterkey',
'database' => 'c:/database.GDB',
'dbdriver' => 'ibase',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'ANSI',
'dbcollat' => 'NONE',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
I'm trying to connect to an SQL server using CodeIgniter. If I use the sqlsrv driver - I get a fatal error message and if I use the odbc driver - I get an 'Unable to connect to your database server using the provided settings error message. Does anyone know how to fix this problem??? I don't mind how, I just want Codeigniter to connect to the SQL Server Database.
This is the database config file
$db['otherdb']['hostname'] = '195.234.10.55\SQLEXPRESS';
$db['otherdb']['username'] = 'username';
$db['otherdb']['password'] = 'password';
$db['otherdb']['database'] = 'ONEDB';
$db['otherdb']['dbdriver'] = 'odbc'; // Done this in both ODBC and SQLSRV
$db['otherdb']['dbprefix'] = '';
$db['otherdb']['pconnect'] = TRUE;
$db['otherdb']['db_debug'] = TRUE;
$db['otherdb']['cache_on'] = FALSE;
$db['otherdb']['cachedir'] = '';
$db['otherdb']['char_set'] = 'utf8';
$db['otherdb']['dbcollat'] = 'utf8_general_ci';
$db['otherdb']['swap_pre'] = '';
$db['otherdb']['autoinit'] = TRUE;
$db['otherdb']['stricton'] = FALSE;
Thanks
(Don't mean to resurrect an old post, but just in case others are looking for the answer, and because this post came up during my search...)
A short step-by-step guide to connect to SQL_server from CodeIgniter 3 using native driver:
First of all download PHP for SQL_server Microsoft native driver from http://www.microsoft.com/en-us/download/details.aspx?id=20098. Take care to choose the one that fits your setup.
Install it to the PHP extension directory.
Configure the new extension in PHP.ini. uncommenting or appending the line (depending on the choosen driver version). I.e.:
extension = php_sqlsrv_55_ts.dll
Restart Apache.
Now your PHP setup is able to connect to SQL_server databases.
Set up your CodeIgniter database connection by configuring application/config/database.php like
<?php
// native-driver connection
$db['default'] = array(
'dsn' => '',
'hostname' => 'SERVER\SQLEXPRESS',
'username' => 'test_user',
'password' => 'test_password',
'database' => 'test01',
'dbdriver' => 'sqlsrv',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => 'application/cache',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'autoinit' => TRUE,
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
Tested from CodeIgniter 3.0.1 on SQL_server 2008 R2 and SQL_server 2014 express.