How can I call my stored procedure in CodeIgniter? - php

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
);

Related

switch between two databases for different users in codeigniter

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?

SQL server 2005 connection in CodeIgniter

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);
}
}

How to connect odbc database using codeigniter?

$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

Code igniter : CI_DB_mysqli_driver could not be converted to string

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"...

How to configure two database in codeigniter.?

I am setting my two database like this
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'sql.domain.com',
'username' => 'u_name',
'password' => 'pass',
'database' => 'DB_1',
'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['User_DB'] = array(
'dsn' => '',
'hostname' => 'sql.domain.com',
'username' => 'u_name',
'password' => 'pass',
'database' => 'DB_2',
'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
);
I am accessing above databases in controller using below code.
public function index(){
$db_anal = $this->load->database('User_DB', TRUE);
$p_name = $this->input->post('p_name');
$user = $this->session->userdata('E_Id');
$tabs_data['res1'] = $this->db->distinct()->select('p_type')->from('tab_1')->get()->result();
//$paper_name = $data;
$UP_array = array(
'Status' => 'success',
'User_id' => $user,
);
$query1['res1'] = $this->db->select('*')->from('tab_1')->where('p_type',$p_name)->get();
$query1['res2'] = $this->db->select('*')->from('tab_2')->where($UP_array)->get();
$query1['res3'] = $db_anal->select('*')->from('tab_3')->where('User_id',$user)->get();
echo json_encode($query1);
return true;
}
I am getting null value by using above code any one can tell me. Is it problem with second database.
How can I solve this any one can help me.?
I am getting below null in console also am using ajax to get data from codeigniter.
{"res1":{"conn_id":{"affected_rows":null,"client_info":null,"client_version":null,"connect_errno":null,"connect_error":null,"errno":null,"error":null,"error_list":null,"field_count":null,"host_info":null,"info":null,"insert_id":null,"server_info":null,"server_version":null,"stat":null,"sqlstate":null,"protocol_version":null,"thread_id":null,"warning_count":null},"result_id":{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null},"result_array":[],"result_object":[],"custom_result_object":[],"current_row":0,"num_rows":null,"row_data":null},"res2":{"conn_id":{"affected_rows":null,"client_info":null,"client_version":null,"connect_errno":null,"connect_error":null,"errno":null,"error":null,"error_list":null,"field_count":null,"host_info":null,"info":null,"insert_id":null,"server_info":null,"server_version":null,"stat":null,"sqlstate":null,"protocol_version":null,"thread_id":null,"warning_count":null},"result_id":{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null},"result_array":[],"result_object":[],"custom_result_object":[],"current_row":0,"num_rows":null,"row_data":null},"res3":{"conn_id":{"affected_rows":null,"client_info":null,"client_version":null,"connect_errno":null,"connect_error":null,"errno":null,"error":null,"error_list":null,"field_count":null,"host_info":null,"info":null,"insert_id":null,"server_info":null,"server_version":null,"stat":null,"sqlstate":null,"protocol_version":null,"thread_id":null,"warning_count":null},"result_id":{"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null},"result_array":[],"result_object":[],"custom_result_object":[],"current_row":0,"num_rows":null,"row_data":null}}
teq:863:6
getting everything null any one can help me.?
the problem is the CI Code
According to the docs, get returns a (query-)result object
If you want the data out of it try the following
$objQuery = $db_anal->select('*')->from('tab_3')->where('User_id',$user)->get();
if ($objQuery->num_rows() > 0)
{
$query1['res3'] = $objQuery->result();
}
According to the latest CI Docs for using multiple database connection you should not call DB with $this->db->query() but with returned DB object for each connection you make like in your exmaple:
$db_anal = $this->load->database('User_DB', TRUE);
This TRUE flag is indicating to the CI to return DB object and you are storing it in $db_anal so now you sholud use this DB connection as $db_anal->query()...
Change in your code all calls like this:
$tabs_data['res1'] = $this->db->distinct()->select('p_type')->from('tab_1')->get()->result();
to this:
$tabs_data['res1'] = $db_anal->distinct()->select('p_type')->from('tab_1')->get()->result();

Categories