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
Related
I am working on project with codeigniter, all working properly only during one table operations, its showing error "table does not exist". Actually that table is present in database. Everything working properly on localhost. Only causing error "table does not exist" for only one table. Otherwise remaning things in project are working properly on live also.
I tried with following way:
$db['default']['pconnect'] = TRUE; $db['default']['db_debug'] = TRUE;
are changed to FALSE.. then its showing "page is not responding" issue.
My Code Snippet (Of Model File):
public function getClubmember($edit_id = 0)
{
$this->db->select(" * ");
$this->db->from('XXXXXXX as XX');
if($edit_id){
$this->db->where('XX.id',$edit_id);
}
$this->db->order_by('XX.id', 'desc');
$result = $this->db->get();
return $result->result_array();
}
My Config Code:
....
....
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'XXXXXXX';
$db['default']['password'] = 'XXXXXXX';
$db['default']['database'] = 'XXXXXXX';
$db['default']['dbdriver'] = 'mysqli';
$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;
....
Please suggest me the changes to resolve issue of "table does not exists". Only issue one table.. remaining project working fine.
I also faced such issue, you just need to check table name. It should be as it is. If one of the character may be capital in table of script. At such situation, task work properly on local, but give such error on live.
Just check script table name spell. Is there any letter capital or small because of which not matching with db table name.
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...';
I am trying to read a existing sqlite database file in codeigniter using PDO connection. My question is actually two fold:
Reading the existing sqlite database actually creates a new db file along with the original one. assume the original db filename is a.db, the new one will be a.db;dbname=.
Subsequent db statements operate on the a.db;dbname=. I have tried to run create table, insert new value on this new db file. both run fine. However, select statement returns nothing and no error either.
I am baffled at this point.. Please help.
updated with the related settings:
// sql
$this->db->query('create table x (y int)');
$this->db->query('insert into x values(6)');
$result = $this->db->get('x')->result(); // $result is an empty array
// config/database.php
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'sqlite:/home/perlwle/db/a.db';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['database'] = '';
$db['default']['dbdriver'] = 'pdo';
$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;
/home/perlwle/db/a.db is the sqlite database file and owned and writable by www-data.
thanks.
This is a bug in CodeIgniter; you have to update.
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.