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);
Related
I am new to codeigniter, and I want to connect to two databases to get the values in the same file. I googled around a bit and found that we can do that by using:
$dsn1 = 'dbdriver://user:pass#ip/db1';
$dsn2 = 'dbdriver://user:pass#ip/db2';
$DB1 = $this->load->database($dsn1,TRUE);
$DB2 = $this->load->database($dsn2,TRUE);
Now this gives the error saying that
dbdriver_driver.php file is not present.
This is a fresh installation.
And I will post the contents of the database folder shortly.
Edit: There is no drivers folder in the database directory
Edit2: It says no such file or directory, and that is true. Should I install addons for this to work or did I somewhere make a mistake or forget a part during installation of codeigniter
Edit 3: The database.php file:
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$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;
The hostname, username and password are set as well.
You're mixing apples with potatoes here. This is how your config/database.php should look like:
$active_group = 'db1'; // the default db connection.
$active_record = TRUE;
$db['db1']['hostname'] = 'localhost';//or ip
$db['db1']['username'] = 'username';
$db['db1']['password'] = 'pass';
$db['db1']['database'] = 'db_name';
$db['db1']['dbdriver'] = 'mysql';
$db['db1']['dbprefix'] = '';
$db['db1']['pconnect'] = TRUE;
$db['db1']['db_debug'] = TRUE;
$db['db1']['cache_on'] = FALSE;
$db['db1']['cachedir'] = '';
$db['db1']['char_set'] = 'utf8';
$db['db1']['dbcollat'] = 'utf8_general_ci';
$db['db1']['swap_pre'] = '';
$db['db1']['autoinit'] = TRUE;
$db['db1']['stricton'] = FALSE;
// the second db connection configuration
$db['db2']['hostname'] = 'localhost';//or ip
$db['db2']['username'] = 'username';
$db['db2']['password'] = 'pass';
$db['db2']['database'] = 'db_name';
$db['db2']['dbdriver'] = 'mysql';
$db['db2']['dbprefix'] = '';
$db['db2']['pconnect'] = TRUE;
$db['db2']['db_debug'] = TRUE;
$db['db2']['cache_on'] = FALSE;
$db['db2']['cachedir'] = '';
$db['db2']['char_set'] = 'utf8';
$db['db2']['dbcollat'] = 'utf8_general_ci';
$db['db2']['swap_pre'] = '';
$db['db2']['autoinit'] = TRUE;
$db['db2']['stricton'] = FALSE;
So you have two database connections configured. Now, you must use them like this, inside controllers or model functions:
$DB1 = $this->load->database('db1',TRUE);
$DB2 = $this->load->database('db2',TRUE);
EDIT:
Your DSN variable should have everything needed to establish a connection, it will not look inside config/database.php file. I suppose you diddn't actually change 'dbdriver' key-word in $dsn1 = 'dbdriver://user:pass#ip/db1; string to the actual db engine name (?), like 'mysql', 'msqli', 'pdo' or whatever engine you use.
So if you write 'dbdriver', CI expects to find a folder named 'dbdriver'(with that exact name) inside 'system/database/drivers/' directory, and a php file named 'dbdriver_driver.php'. If you write 'mysql', it will find the mysql driver file : 'system/database/drivers/mysql/mysql_driver.php'.
The other configuration variables, like pconnect, db_debug or whatever, should be mentioned as a query string:
$dsn1 = 'mysql://user:pass#ip/db1?pconnect=true&db_debug=false&etc...';
$dsn2 = 'pdo://user:pass#ip/db1?pconnect=true&db_debug=false&etc...';
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
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 want to change some database settings for one part of my program.
In my setup the databse class is autoloaded with a config which looks like this
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '****';
$db['default']['database'] = 'database';
$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;
At one part of the script I want to change the value of $db['default']['keyname'] , how can I do this?
You should be adding another set of credentials instead of changing the existing ones dynamically:
$db['another_db']['hostname'] = 'localhost';
$db['another_db']['username'] = 'root';
$db['another_db']['password'] = '****';
$db['another_db']['database'] = 'database';
$db['another_db']['dbdriver'] = 'mysql';
$db['another_db']['dbprefix'] = '';
$db['another_db']['pconnect'] = TRUE;
$db['another_db']['db_debug'] = TRUE;
$db['another_db']['cache_on'] = FALSE;
$db['another_db']['cachedir'] = '';
$db['another_db']['char_set'] = 'utf8';
$db['another_db']['dbcollat'] = 'utf8_general_ci';
$db['another_db']['swap_pre'] = '';
$db['another_db']['autoinit'] = TRUE;
$db['another_db']['stricton'] = FALSE;
You can load this other database by doing:
$another_db = $this->load->database('another_db', TRUE);
Then use it like normal database driver:
$another_db->select();
...etc
There is how class > check it http://codeigniter.com/user_guide/libraries/config.html
How ever you can NOT change db connection settings.... They are loaded long before your configs..
Slightly hacky, but you could add something like this to system/database/DB_driver.php (I've used db password change as an example):-
public function update_pw($value) {
$this->password = $value;
}
Then in your project, do
$this->db->update_pw('example');
$this->db->reconnect();
Depending on specifically what you want to change in the config - and also more importantly, why you want to change it - this may or may not be the best option.
Also you can always use a Global var. Normally I always have 2 databases in place. One for production and one for development. You just check that var to load one or the other.
That concept can be used to load one database in one part of your application and other in other part. However, I would go with the built in solution like Brendan pointed out.