Hello All i am connecting SQL server as a second database IN MY CODEIGNITER project the database is connected but i am not getting the data below is my configuration file and select query.
$db['database2'] = array (
'hostname' => 'SERVER',
'username' => 'sa',
'password' => 'DEMO',
'database' => 'naaz',
'dbdriver' => 'sqlsrv',
'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,
'autoinit' => TRUE,
'failover' => array(),
'save_queries' => TRUE
);
--controller
class Api extends CI_Controller {
$db2 = '';
public function __construct()
{
parent::__construct();
$this->db2 = $this->load->database('database2',TRUE);
}
public function index() {
$query = $this->db2->query('SELECT * FROM TIMEDATA');
$result = $query->result_array();
print_r($result);
}
}
Related
I need to change the database name in database.php passing value from the controller. I tried sessions and env variable. but in the database.php cannot access the value from sessions and env variable. also, i need to use the default database to load the page and when user has been logged in need to switch the database. i'm doing this for reducing size of the database data is there any solution really glad someone can help me.
databse.php
$active_group = 'default';
$query_builder = TRUE;
if($db_val==""){
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'sliate_srs',
'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
);
}
else{
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => getenv("DB_year"),
'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
);
}
controller.php
function loginSubmit() {
$DB_year=$this->input->post('year_select');
//$this->session->unset_userdata('DB_year');
//$this->session->set_userdata('DB_year',$DB_year);
$DB_year="srs_2019";
putenv("DB_year=$DB_year");
// $_ENV["DB_year"]="srs_2019";
print_r(getenv("DB_year"));
// $DB_year="srs_2019";
$result = $this->Login_model->authenticateLogin();
if (!empty($result)) {
$now = date('Y-m-d H:i:s');
$ip = $this->input->ip_address();
$data = array(
'u_id' => $this->session->userdata('u_id'),
'u_name' => $this->session->userdata('u_name'),
'center_name' => $this->session->userdata('u_branch'),
'last_login_ip' => $this->input->ip_address('ip'),
'last_login_date_time' => $now
);
$name = $this->session->userdata('DB_year');
print_r($name);
$this->Login_model->last_login($data);
redirect('Admin/dashboard');
} else {
redirect('Login?login=invalid');
}
}
Instead of changing the config group in Database.php, why not create a custom connection in your model, as explained in Connecting with Custom Settings?
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => 'tes',
'hostname' => 'localhost',
'username' => '',
'password' => '',
'database' => 'tes',
'dbdriver' => 'odbc',
'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
);
When I run the program there is an error:
" Message: Call to undefined method CI_DB_odbc_driver::select() Filename: models/m_city.php"
m_city.php file:
<?php
class m_city extends CI_Model
{
function get_all($where = array())
{
$this->db->select('Name,Population');
$this->db->where($where);
$this->db->limit('50');
$query = $this->db->get('City');
return $query->result_array();
}
}
I already made dsn name on odbc. the name of dsn is "tes".
If you use "Mysql" then follow the following instructions.
Please put database username and database password in the following:
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => '',//Put database username
'password' => '',// put database password
'database' => 'tes',
'dbdriver' => 'odbc',
'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
);
After complete this save the page and open the page 'autoload.php' under the config folder. There search "$autoload['libraries']" option. Put the following
$autoload['libraries'] = array('database');
If you use PDO, PostgreSQL, Oracle then see the following links:
https://www.codeigniter.com/user_guide/database/configuration.html
I need to connect two databases in my application. One is MySQL DB and the other one is SQL Server DB. Both hosted in IIS. The connection to MySQL DB is ok, but when I try to connect to SQL DB it shows some error like
Call to undefined function sqlsrv_connect()
The following is my database.php file
$active_group = 'default';
$db['default'] = array(
'dsn' => '',
'hostname' => 'xxxxx',
'username' => 'xxxxx',
'password' => 'xxxxx',
'database' => 'mydb',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => FALSE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
$db['voiceengine'] = array(
'dsn' => '',
'hostname' => 'xxxxx',
'username' => 'xxxxx',
'password' => 'xxxxx',
'database' => 'db1',
'dbdriver' => 'sqlsrv',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => FALSE,
'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 My_model.php is
function checkFile($file) {
$this->db->where('FileName', $file);
return $this->db->count_all_results('voicefile_tbl');
}
function scheduleVoiceSMS($data) {
$voiceengine = $this->load->database('voiceengine', TRUE);
$scheduleData = array();
$this->db->select('Name, Duration');
$this->db->where('FileName', $data['file']);
$result = $this->db->get('voicefile_tbl')->row_array();
if(isset($data['sch_time']) && $data['sch_time'] != ""){
$campaignData = array(
'vaCampaignName' => $result['Name'],
'vaFileName' => $data['file'],
'intFileDurationInSeconds' => $result['Duration'],
'dtScheduledDateTime' => date('Y-m-d H:i:s', strtotime($data['sch_time'])),
'intUserID' => $data['UserID']
);
}
$voiceengine->insert('Campaign', $campaignData);
$id = $this->db->insert_id();
$mobiles = explode(',', $data['destinations']);
foreach($mobiles as $mobile){
if (strlen($mobile) == 12 && substr($mobile, 0, 2) == "91")
$mobile = substr($mobile, 2, 10);
$campaignData = array(
'intCampaignID' => $id,
'intMobileNumber' => $mobile);
array_push($scheduleData, $campaignData);
}
$voiceengine->insert_batch('CampaignNumbers', $scheduleData);
}
I have installed the dll file, and the extension added in php.ini file. But I cannot connect to the database
I have a existing users database from another database and want to use it on my new site using codeigniter with a new database both is running on mysql, i configured my database.php like below, and configure another database connection.
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => 'root',
'database' => 'new_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
);
$db['otherdb'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => 'root',
'database' => 'members_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
);
Now i am trying to query the username and password on my Users_model.php my function is like this.
public function login($username, $password){
$otherdb = $this->load->database('otherdb', TRUE);
$this->$otherdb->where('user_name', $username);
$this->$otherdb->where('user_pass', $password);
$result = $this->$otherdb->get('members');
if($result->num_rows() == 1){
return $result->row(0)->ID;
}else{
return false;
}
}
But got a error below, i am new with codeigniter and not sure if this is the proper way of querying from another database, any advice would help! thanks in advance!
Object of class CI_DB_mysqli_driver could not be converted to string
Change this
$otherdb = $this->load->database('otherdb', TRUE);
To
$this->otherdb = $this->load->database('otherdb', TRUE);
It will work.
Compa, you have an error when you call
$result = $this->$otherdb->get('members');
The error is with the $this - you have to do:
$result = $otherdb->get('members');
Good luck.
PS: I also had a similar error
Change this $this->$otherdb To $otherdb->where and $otherdb->get without "$this->" because you are working on "otherdb"...
I want to call my stored procedure in CodeIgniter, but nothing works. I added a new function called free_db to mysqli_result, but it still doesn't work. It gives me the result, but the error persists. How can I fix this?
Controller:
public function best_selling() {
$data = $this->graph->getGraph();
print_r($data);
}
Model:
public function getGraph() {
$data = $this->db->query('CALL storelte_best_selling()');
return $data->result_array();
}
Setting database:
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => '**********',
'password' => '*********',
'database' => 'storelte',
'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
);