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
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 am using CI and my database is on dream host. And when i connect database. it gives error like this
A Database Error Occurred
Unable to connect to your database server using the provided
settings.
Filename:
/home/demo_smartmobe/demos.smartmobe.com/nayacinema/webpart/third_party/MX/Loader.php
Line Number: 98
my ci code for database connection is
$db['default']['hostname'] = 'hostname';
$db['default']['username'] = 'username';
$db['default']['password'] = '******';
$db['default']['database'] = 'database_name';
$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['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
username and password is correct.
and i had tried by simple php code and it works. Php Code is here.
$hostname = "mysql.demos.smartmobe.com"; // eg. mysql.yourdomain.com (unique)
$username = "nayacinema"; // the username specified when setting-up the database
$password = "****"; // the password specified when setting-up the database
$database = "nayacinema"; // the database name chosen when setting-up the database (unique)
$con=mysqli_connect($hostname,$username,$password,$database);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
echo 'done';
}
$result = mysqli_query($con,"SELECT * FROM TblUsers");
print_r($result);
while($row = mysqli_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br>";
}
what may be the problem? I had add MX folder in Ci folder for make it HMVC. And error is shown in /third_party/MX/Loader.php. IN localhost in works but in live there is error .please help me. Thank u
You stated your username and password are correct, but what about the hostname?
Modify your hostname setting to be your correct hostname.
E.g.
$db['default']['hostname'] = 'localhost';
or
$db['default']['hostname'] = '127.0.0.1';
You will also need to change $db['default']['database_name'] to match the name of the database you're attempting to use.
You need to get the correct database connection info from Dreamhost. You can find the ones that you have set up for that domain in your Dreamhost Web Panel, under the MySQL Databases tab, on the left side. Under that page, you can find the hostname, database, and username. If you click on the username that has access to the database, you can find out what your password is. Put those values into your database.php config file, like in the fields show below.
$db['default']['hostname'] = 'mysql.example.com';
$db['default']['port'] = "3306";
$db['default']['username'] = 'mysql_username';
$db['default']['password'] = 'mysql_password';
$db['default']['database'] = 'mysql_database';
First of all, if you are accessing the database within the same pc.
(localhost and 127.0.0.1 are the same)
If not, the host name should be identified by DNS Name or with domain name included.
And if the database port is not the default (3306), you should include it on the config.
Like the code in your php:
$db['default']['hostname'] = 'mysql.demos.smartmobe.com';
$db['default']['username'] = 'nayacinema';
$db['default']['password'] = '******';
$db['default']['database'] = 'nayacinema';
$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['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
//$db['default']['port'] = 3307; //modify if the port is not the default 3306
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
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);
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);