title: CodeIgniter - Using FlashData WITH Sessions Stored in database
body: <p>My configuration on CI is pretty standard.</p>
  
  <pre><code>$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;
  </code></pre>
  
  <p>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).</p>
  
  <p>I now have a new bug which I believe has to do with my code but I'm not sure how to fix it.</p>
  
  <p>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.</p>
  
  <pre><code>$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;
  </code></pre>
  
  <p>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.</p>
  
  <p><strong>Update:</strong> 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.</p>
  
  <p><strong>Update 2:</strong> 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...</p>
  
  <p><strong>Update 3:</strong> 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...</p>
  
  <pre><code>ERROR - 2012-11-23 15:38:39 --&gt; "logged_in";b:1;}
  ERROR - 2012-11-23 15:38:39 --&gt;"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.";}}
  </code></pre>
  
  <p><strong>SOLVED</strong>
  Thanks to mohan.gade I've found a solution.</p>
  
  <p>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.</p>
  
  <pre><code>$this-&gt;db1 = $this-&gt;load-&gt;database('db1', TRUE);
  </code></pre>
  
  <p>So the fix...although not fun...is to load all databases manually when working with multiple connections.</p>
  
  <pre><code>$this-&gt;db = $this-&gt;load-&gt;database('default', TRUE);
  $this-&gt;db1 = $this-&gt;load-&gt;database('db1', TRUE);
  </code></pre>
  
  <p>Thanks!</p>