Code igniter : CI_DB_mysqli_driver could not be converted to string - php

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

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?

Codeigniter connect to second DB based on first DB

I have an autoloaded DB which has all its var set in the config/database.php file as a default group:
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => 'root',
'database' => 'dbname',
'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
);
In this default DB there is a table where I need to read an external DB, and I have all fields to get a new connection.
I need to connect both DB at the same time, but I cannot define the DB variables in the config/database.php as these are dynamic and may change depending on the DB (default) content.
My idea was this either to SET $db['external'] = [...] IN THE CONTROLLER and set the data from the default DB I read, or simply use a DNS:
Solution #1:
public function wordpress()
{
$DB = $this->load->database('default', true);
$wp_db = $DB->get_where('dbtable', ['type_needed' => 'wordpress'])->row();
$db['wp_db'] = array(
'dsn' => '',
'hostname' => $wp_db->mysql_host,
'username' => $wp_db->mysql_user,
'password' => $wp_db->mysql_password,
'database' => $wp_db->mysql_db,
'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,
);
$this->WPDB = $this->load->database('wp_db', true);
}
Solution #1 gives "You have specified an invalid database connection group (wp_db) in your config/database.php file." error
Solution #2:
$DB = $this->load->database('default', true);
$wp_db = $DB->get_where('dbtable', ['type_needed' => 'wordpress'])->row();
$wp_dns = "mysql://$wp_db->mysql_user:$wp_db->mysql_password#$wp_db->mysql_host/$wp_db->mysql_db";
$this->WPDB = $this->load->database($wp_dns, true);
Solution #2 gives a "Invalid DB Connection String" error
Ps: I'm moving to Laravel, but this project was built with CI already :)
the only thing you've to change in your function is the following
public function wordpress()
{
$DB = $this->load->database('default', true);
$wp_db = $DB->get_where('dbtable', ['type_needed' => 'wordpress'])->row();
$arrDbData = array(
'dsn' => '',
'hostname' => $wp_db->mysql_host,
'username' => $wp_db->mysql_user,
'password' => $wp_db->mysql_password,
'database' => $wp_db->mysql_db,
'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,
);
$this->WPDB = $this->load->database($arrDbData, true);
}
I found out Solution #2 is working but due to the dns connection which is a STRING you are to make sure the password is made of letters and numbers and NO SYMBOLS otherwise it screws up the string and does not read properly.
In my case the password was this one iidf#q0RDTh#)CrPo5PDLeVe so dashes and parenthesis created a problem where CI could not read the whole password.

Queries on multiple database with codeIgniter

I have 2 databases, and I would like to make a query with the 2 databases, like for example
SELECT base1.table1.item1 FROM base1.table1 INNER JOIN base2.table3 ON base2.table3.item2 = base1.table1.item2 WHERE base2.table3.item4 = 'toto';
How to make this query with codeIgniter ?
I already have configured database.php in CodeIgniter with the 2 databases.
Thanks.
You can setup 2 database in config/database.php file
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'first_db',
'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
);
//set second db configuration
$db['otherdb'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'second_db',
'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
);
When you want use default database means master database
// use master dataabse
$users = $this->db->get('users');
// connect to secondary database
$otherdb = $this->load->database('otherdb', TRUE);
$data = $otherdb->get('table_name');
if your first db name is base1 and second is base2
$this->db->select('table1.item1 FROM table1');
$this->db->from('table1');
$this->db->join('base2.table3', 'base2.table3.item2 =table1.item2');
$this->where('base2.table3.item4','toto')
$query = $this->db->get();

How can I call my stored procedure in CodeIgniter?

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

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