Can I use two mysql users on Codeigniter? - php

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

Related

CodeIgniter: Failed opening required '<<document_root>>/system/database/drivers/dbdriver/dbdriver_driver.php'

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...';

Database Error Occurred

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

CodeIgniter - Using FlashData WITH Sessions Stored in database

My configuration on CI is pretty standard.
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 0;
$config['sess_expire_on_close'] = TRUE;
$config['sess_encrypt_cookie'] = TRUE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = TRUE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 2000;
I don't use expire because I am testing the system, I know it's not good practice. I use a high sess_time_to_update because of AJAX requests destroying the session (a CI bug).
I now have a new bug which I believe has to do with my code but I'm not sure how to fix it.
I've been storing my sessions in a database using one database connection. The flashdata to display error messages has been working perfectly.. until recently. Now the flashdata wont set when I introduced a second database connection.
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'emp';
$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;
$db['dil']['hostname'] = 'localhost';
$db['dil']['username'] = 'root';
$db['dil']['password'] = '';
$db['dil']['database'] = 'dil';
$db['dil']['dbdriver'] = 'mysql';
$db['dil']['dbprefix'] = '';
$db['dil']['pconnect'] = TRUE;
$db['dil']['db_debug'] = TRUE;
$db['dil']['cache_on'] = FALSE;
$db['dil']['cachedir'] = '';
$db['dil']['char_set'] = 'utf8';
$db['dil']['dbcollat'] = 'utf8_general_ci';
$db['dil']['swap_pre'] = '';
$db['dil']['autoinit'] = TRUE;
$db['dil']['stricton'] = FALSE;
After I set my flash data I display it with var_dump and print_r and it prints a boolean: false. When I don't use a second connection it works fine. Any ideas? Some people say to not store sessions in DB but I need to for security.
Update: I that assumed the set_flashdata didnt insert it into the database so I checked to be sure and that is the problem. set_flashdata is not inserting the flash data. Other pages with two connections works fine (the notifications) just this page isnt.
Update 2: When I set the flashdata and exit; right after i look in the database and there is a message: "You need to login to see this page". This is set when someone isnt logged in. Hm.. definitely something weird going on...
Update 3: after using the log_message( in the SESS_WRITE( function in the Core/libraries/session file I see that it sets the message correctly but then there is an extra refresh some how...
ERROR - 2012-11-23 15:38:39 --> "logged_in";b:1;}
ERROR - 2012-11-23 15:38:39 -->"logged_in";b:1;s:22:"flash:new:notification";a:2:{s:4:"type";s:7:"success";s:7:"message";s:31:"Image was updated successfully.";}}
SOLVED
Thanks to mohan.gade I've found a solution.
When working with multiple databases with the way I set mine up (default, db1, db2 etc..) when you load a new database into a variable when you autoload the default database it resets the $this->db.
$this->db1 = $this->load->database('db1', TRUE);
So the fix...although not fun...is to load all databases manually when working with multiple connections.
$this->db = $this->load->database('default', TRUE);
$this->db1 = $this->load->database('db1', TRUE);
Thanks!
How doyou make database connection ? Is config/autoload library contains 'database' ? then remove it . do the database conncetion manually.
If you need to connect to more than one database simultaneously you can do so as follows:
$emp = $this->load->database('emp', TRUE);
$dil = $this->load->database('dil', TRUE);
And access the database functions following way
$emp->query();
insted of regular method $this->db->query()
for more details go to the http://codeigniter.com/user_guide/database/connecting.html

Codeigniter - how do I update my database config settings dynamically?

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.

How do i log inside database with separate passwords?

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

Categories