I'm diving into multiple database usage. According to the codeigniter user guide. To connect to the additional databases use use the following
$db2 = $this->load->database('second');
then to interact use,
$db2->get('second_table');
I'm receiving a Fatal error call to a member function "where()" on a non-object.
for the following line
$db2->where('field1', $data['item']);
and also for
$db2->get('second_table');
Where am I going wrong with this?
Thanks for any help.
In order to return the database object, you need to pass a TRUE as second paramenter:
$db2 = $this->load->database('second', TRUE);
See the manual on database class for more info.
Make also sure you've loaded the config for that database in application/config/database.php
$db['default']['hostname'] = 'localhost';
//.........
$db['second']['hostname'] = 'localhost';
//..........
In config/database.php
/
* DB1 */
$active_group = "forum";
$active_record = TRUE;
$db['DB1']['hostname'] = "xxxxx";
$db['DB1']['username'] = "xxxxx";
$db['DB1']['password'] = "xxxxx";
$db['DB1']['database'] = "xxxxx";
and other configs....
/* DB2 */
$db['DB2']['hostname'] = "xxxxx";
$db['DB2']['username'] = "xxxxx";
$db['DB2']['password'] = "xxxxx";
$db['DB2']['database'] = "xxxxx";
$db['DB2']['dbdriver'] = "mysql";
$db['DB2']['dbprefix'] = "";
and so on...
you can use databases by
$this->DB1 = $this->CI->load->database('DB1', TRUE);
$this->DB2 = $this->CI->load->database('DB2', TRUE);
Related
i am getting this error as i integrate this forum in my website i am new to this error his is my config.php i had made changes according to my database i am new to this i don't know why and what is this error.
<?php
$dbms = 'phpbb\\db\\driver\\mysqli';
$db host = 'localhost';
$dbport = '';
$dbname = 'abc_forum';
$dbuser = 'root',
$dbpasswd = '';
$table_prefix = '';
$phpbb_adm_relative_path ='adm/';
$acm_type = 'phpbb\\cache\\driver\\file';
#define('PHPBB_INSTALLED', true);
change $db host to $dbhost :
<?php
$dbms = 'phpbb\\db\\driver\\mysqli';
$dbhost = 'localhost';
$dbport = '';
$dbname = 'abc_forum';
$dbuser = 'root',
$dbpasswd = '';
$table_prefix = '';
$phpbb_adm_relative_path ='adm/';
$acm_type = 'phpbb\\cache\\driver\\file';
#define('PHPBB_INSTALLED', true);
The username and password are not valid. That is what this means. Ask your hosting provider for the correct credentials.
Keep it simple
$conn = new mysqli("DB_HOST","USERNAME","PASSWORD","DATABASE");
I've just downloaded cacti and I kept getting
FATAL: Cannot connect to MySQL server on 'localhost'. Please make sure you have specified a valid MySQL database name in 'include/config.php'
This is what I have in include/config.php
$database_type = "mysql";
$database_default = "cacti";
$database_hostname = "localhost";
$database_username = "root";
$database_password = "***";
$database_port = "3306";
$database_ssl = false;
What did I forget or did wrong ?
is there a way to reconfig database using a controller in codeigniter?
let say i have this code in database config
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'db';
Then i want to change to this
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'admin';
$db['default']['password'] = 'password';
$db['default']['database'] = 'db_admin';
Using controller in codeigniter. Is this possible?
You can define multiple database in database.php in config folder like
//database 1 details
$db['db1']['hostname'] = 'localhost';
$db['db1']['username'] = 'root';
$db['db1']['password'] = '';
$db['db1']['database'] = 'db';
//database 2 details
$db['db2']['hostname'] = 'localhost';
$db['db2']['username'] = 'admin';
$db['db2']['password'] = 'password';
$db['db2']['database'] = 'db_admin';
And you can connect to required database as following in your scripts
if(condition){
$DB = $this->load->database('db1', TRUE);
}
else{
$DB = $this->load->database('db2', TRUE);
}
When you connect database like this, you have to use your object name $DB rather than $this
$DB->query();
$DB->result();
instead of
$this->db->query();
$this->db->result();
For more details
Yes you can do that. Inside your controller function specify the conditions and then add the following code:
$config['hostname'] = "localhost";
$config['username'] = "admin";
$config['password'] = "password";
$config['database'] = "db_admin";
$this->load->database($config);
Now you can access the new database config. Also disable caching as,
$db['default']['cache_on'] = FALSE;
in your database.php file inside config folder.
Hope it helps.
Looks like i have found the solution. Dont know is better or not. But its better than nothing.
I have tried over
$config['hostname'] = "localhost";
$config['username'] = "admin";
$config['password'] = "password";
$config['database'] = "db_admin";
$this->load->database($config);
and tried many ways.. but its always call the database from config folder
Then i found this basic php method for calling the database i desire
like this
$dbname = 'my_db';
$mysqli = new mysqli("localhost", "root", "", $dbname);
$result = $mysqli->query("query")`
$result->close();
I know how foolish i am, and how silly the answers.. but its better than nothing hahaha
In codeigniter, I use one database which contains two MySQL users, and now I want to know if it is possible to use two MySQL users in one database in codeigniter..
Yes you can!
Use 2 Database connections to SAME DB with different username / password !
http://ellislab.com/codeigniter/user-guide/database/connecting.html
You have to define a second set of database parameters. CI isn’t developed to really have two DB connections though, it is more for swapping test and production dbases. That said there are some tricks around it. So first define a second set of DB info like so:
/* FORUM */
$active_group = "forum";
$active_record = TRUE;
$db['forum']['hostname'] = "xxxxx";
$db['forum']['username'] = "xxxxx";
$db['forum']['password'] = "xxxxx";
$db['forum']['database'] = "xxxxx";
$db['forum']['dbdriver'] = "mysql";
$db['forum']['dbprefix'] = "";
$db['forum']['pconnect'] = TRUE;
$db['forum']['db_debug'] = TRUE;
$db['forum']['cache_on'] = FALSE;
$db['forum']['cachedir'] = "";
$db['forum']['char_set'] = "utf8";
$db['forum']['dbcollat'] = "utf8_general_ci";
/* TEST SITE */
$active_group = "default";
$active_record = TRUE;
$db['default']['hostname'] = "xxxxx";
$db['default']['username'] = "xxxxx";
$db['default']['password'] = "xxxxx";
$db['default']['database'] = "xxxxx";
$db['default']['dbdriver'] = "mysql";
$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";
Your active db will be the one you defined LAST.
Once you have done this you can manually connect to the second one (or put it in MY_Controller if you always need to). You can then load your second database like so:
$this->other_db= $this->CI->load->database('forum', TRUE);
Access dbase 1 with $this->db and dbase 2 with $this->other_db (or whatever you called it).
thank you
That should do it. The documentation for connecting to multiple databases can be found here: http://ellislab.com/codeigniter/database/connecting.html
How do i log inside database with separate passwords? We can configure only one user in database.php. I want that the login process should happen with different mysql username and password because that table is protected.
Thanks in advance:)
By following the user documentation you will find this page:
http://codeigniter.com/user_guide/database/connecting.html
The documentation states that you can connect to multiple databases by specifying a group.
If you look in your database.php file, you will see that the connection arrays is formatted like this:
$db['default']['hostname'] = "localhost";
The 'group' here is default, which is loaded the way you always have been doing:
$this->load->database();
When you need to connect to another database, specify a new group:
$db['my_secret_db']['hostname'] = "localhost";
$db['my_secret_db']['username'] = "other_mysql_user";
...
And you load it like so:
$MyOriginalDb = $this->load->database('default', true);
$MyOtherDb = $this->load->database('my_secret_db', true);
By loading these connections into their own objects, you will now use:
$MyOtherDb->query();
instead of
$this->db->query();
I hope this helps.
create another connection group in your database.php
$active_group = "post";
$active_record = TRUE;
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "user";
$db['default']['password'] = "user-only";
$db['default']['database'] = "something";
$db['default']['dbdriver'] = "mysql";
$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['post']['hostname'] = "localhost";
$db['post']['username'] = "admin";
$db['post']['password'] = "admin-only";
$db['post']['database'] = "something";
$db['post']['dbdriver'] = "mysql";
$db['post']['dbprefix'] = "";
$db['post']['pconnect'] = TRUE;
$db['post']['db_debug'] = TRUE;
$db['post']['cache_on'] = FALSE;
$db['post']['cachedir'] = "";
$db['post']['char_set'] = "utf8";
$db['post']['dbcollat'] = "utf8_general_ci";
create another two model files. one file load the 'default' configuration, and the other load the 'post' configuration.
eg:
$DB1 = $this->load->database('default', TRUE);
$DB2 = $this->load->database('post', TRUE);