0

I'm working on 20 websites and each one has his own database, but they have the same structure of database.

The problem is that I have to login to the different back-end to do some work on each one.

So, I decided to write single CI back-end for all of the websites.

For that, I created a database with two tables (user, websites)

The user table is for the login action.

The websites table list all websites and the associated database.

What I would like to do is, in any view of my backend application, be able to select the database (or website), so that the current view can be reloaded with data from the selected database to perform some work on it and then submit it.

I'm using CI V1.7.2

3
  • codeignter with multi databaes stored on database?What do you mean?
    – Soojoo
    Commented Nov 2, 2012 at 10:52
  • i' like to say that the connections params are stored on my websites table and not on config/database.php , only the connection to login on the back-end is stored in config/database.php
    – NoOneElse
    Commented Nov 2, 2012 at 11:27
  • Add a select box on the landing page where you can pick a website. Once you select the appropriate site save it in a session variable something like: $_SESSION['current_site'] = <ID> OR 'site_name'; And in your base controller switch/select the appropriate database. The select could be on every page for convenience. Commented Nov 2, 2012 at 11:40

1 Answer 1

1

This seems very plausible. However, note that once you change the database adapter, database session storage will not work as you expect since it's stored in the first database that you've mentioned.

So after when you verified the user and grabbed the database's DSN (database source name), you can change the database adapter when executing your queries by doing:

$this->database->load('dbdriver://username:password@hostname/database');

Note that dbdriver is the type of database you are using, refer to the config file for the types. You can also take a look at the documentation for more information.

If you want database session storage to work, you may want to create a new database adapter, the method is similar from above, but there's one more parameter.

$new_db = $this->database->load($dsn, TRUE);

However, this method would most likely require some refactoring in your code since you're used to performing queries like $this->database->query(); and you'll need to change it to $new_db->query().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.