I have two different connection set in the database config file. Once I have connected to the one of the database if I connect to the second database still the connection is not changed. I am getting table not found error.
$this->load->database(); //connecting to default db
$this->db->query('SELECT * from user'); //user table in default db and no error
$this->load->database('second_db');//connecting to second db
$this->db->load('SELECT * from statistic'); //table exists in second db but getting error
//The same work if I comment the first two lines
you can try this way :
$this->load->database('second_db', TRUE);
and also set FALSE for 'pconnect' in database.php :
for default one:
$db['default']['pconnect'] = FALSE;
and for second one:
$db['second_db']['pconnect'] = FALSE;
I have made it work myself.
$this->load->database(); //connecting to default db
$this->db->query('SELECT * from user'); //user table in default db and no error
$this->load->database('second_db',FALSE,TRUE);//connecting to second db
$this->db->load('SELECT * from statistic'); //table exists in second db but getting error
//The same work if I comment the first two lines
The only change is when loading the second database need to pass two extra parameters.
First one FALSE - Don't return connection object
Second TRUE - Change the Active record to the loaded DB
Finally worked for me using Codeigniter 2.1.0:
$second_db = $this->load->database('second_group',TRUE,FALSE);
Making the group constant setting 'pconnect' as:
$db['second_group']['pconnect'] = FALSE;
If you use
$this->load->database('second_db', TRUE);
You have to use like
$db = $this->load->database('second_db', TRUE);
Because the second parameter true return you the the full object
Now the $db is your db object you might assign the $db to a class variable
$this->second_db = $this->load->database('second_db', TRUE);
$this->second_db->query();
Related
i have two database connections. I need to search for both of them.
if the User doesn't exist in the first table and connection, fetch from the second first table and connection.
$customer = User::find($id);
if (!$customer) {
// how to get second fetch connection
}
tables and connections information:
first:
table = users
connection = mysql_1
second:
table = old_users
connection = mysql_2
Try this solution
$customer = User::find($id);
if (!$customer) {
$customer = User::on('other')->find($id);
}
Assuming your tables are the same, and are just in different connections.
First set up another connection in your database config file if you haven't already done so. You will use the array key to access it.
Then you can do:
$customer = User::find($id);
if (!$customer) {
$customer = User::on('automations')->find($id);
}
automations can be whatever you want, so long as it matches your configuration file. This config is just an example from my database config file.
I'm building a CI application, and I need to use 2 different databases (mysql & mssql). When i try to run a simple query to the mssql DB then CI adds the dbprefix to the table names, so it ends up to an error. How is it possible to prevent CI adding the prefix? There is no dbprefix set in database.php file
Query in Model
public function getAccName($uid)
{
$this->load->database('mssqlsrv',TRUE);
return $this->db->where("uid", $uid)
->select("account")->get("user_account");
}
To answer this correctly:
When working with multiple database connections in Codeigniter each connection needs to be assigned to a variable like:
$DB1 = $this->load->database('mssqlsrv', TRUE);
$DB2 = $this->load->database('group_two', TRUE);
Then you can do the query with
$DB1->where("uid", $uid)->select("account")->get("user_account");
instead of
$this->db->where("uid", $uid)->select("account")->get("user_account");
See https://www.codeigniter.com/userguide3/database/connecting.html for more information on this.
How about trying to set each database connection to a certain variable:
like below code:
$mysqlConn = $this->load->database('mysql', TRUE);
$mssqlConn = $this->load->database('mssqlsrv', TRUE);
then try to use them for your query, for the code above it should be:
public function getAccName($uid)
{
$mssqlConn = $this->load->database('mssqlsrv', TRUE);
return $mssqlConn->where("uid", $uid)
->select("account")->get("user_account");
}
I am having trouble using my second database connection is codeigniter.
I have added the connection to database.php
$old = $this->load->database('old_portal', TRUE);
$sql = "SELECT * FROM `frm_root`";
$query = $this->old->query($sql);
But I am getting an error. I am not sure how to use the old object when wanting to use query()
Using multiple databases in CodeIgniter is pretty easy.
https://ellislab.com/codeIgniter/user-guide/database/connecting.html
You would do it like this;
$old = $this->load->database('old', true);
Then, you would access this database object, like this;
$query = $old->query();
Hope this helps.
$query = $old->query($sql);
Suppose i have a function called ssh_connect that takes a server SSH settings as parameters.Now i have multiple servers and i want to choose one of them randomly and input them as parameters into the function.Moreover if $result is empty when using one set of parameters , the other set must be used
$result=ssh_connect($ip, $user, $pass, $port,$command)
Suppose i have the following details
//Server 1
$ip="123.456.78.901";
$user="root"
$pass="mypassishere"
$port = "22"
//Server 2
$ip="225.456.98.901"
$user="root"
$pass="mypassishere"
$port = "22"
I could use selection structures but that solution is heavily flawed as one server will always have priority over the others.It will be like if $result is empty with Server 1 details , move to server 2 , which is really not what i want.
I am thinking of array but i am not sure how it will be done since i need to select a set of details randomly and if one set fails try another one until there are no servers or $result has got a value.
Continued from #gwillie 's answer, here is an example:
<?php
$a = array(
array('host'=>'host1','user'=>'user1','pwd'=>'pwd1'),
array('host'=>'host2','user'=>'user2','pwd'=>'pwd2'),
array('host'=>'host3','user'=>'user3','pwd'=>'pwd3'),
array('host'=>'host4','user'=>'user4','pwd'=>'pwd4'),
array('host'=>'host5','user'=>'user5','pwd'=>'pwd5'),
);
$connect = false;
while (!$connect) {
$srvIndex = array_rand($a);
$host = $a[$srvIndex];
$connect = my_connect_function($host); //Return true on success, false on error
}
I think you are looking for aray_rand, php site states
Picks one or more random entries out of an array,
and returns the key (or keys) of the random entries.
If settings fail, unset the key returned by array_rand(), keep looping till you connect, or fail
I am working with a php program and want to check if a table exists. If it does exist, do nothing, if it doesn't create and populate the table. I ran across
if(mysql_query("DESCRIBE `table`")) {
// Exists
}
but this only takes action if it does exist. Would this
if(!mysql_query("DESCRIBE `table`")) {
// create and populate table
}
do what I am asking?
What you're doing won't work because the test is only testing the return value of the mysql_query() call, not the result of the query itself.
You need to query the tables with SHOW TABLES LIKE 'table' and check the number of rows returned:
$db = new mysqli(...);
$result = $db->query("SHOW TABLES LIKE 'table'");
if ($result->num_rows == 0) {
// create table
}
Note: mysql_*() is deprecated - you shouldn't use it for new code.
You'll find the MySQL reference here, and the PHP reference for mysqli here