Welcome Guest, Not a member yet? Register   Sign In
Dynamic Database Connection


I have 10 db and i want connect on the fly mean if i come from DB1 than my code automatically connect DB1 same with DB2 and DB3.

Define all your databases in config/database.php
$db['db7']['username'] = ...
then load your database with

How do you determine which database to connect to?

Thanks for replying

But i want to know is there any way we can set Database name variable in controller or in model.
Suppose if my URL is http://myurl.com/index.php/welcome/method/AB
Than i want to connect AB database
than XY connected.

Actually i don't no how many database i had, its totally dynamic process thats why i cant define all db name in config variable.

How can it be totally dynamic?

Your database needs to be created, it has a username, password, server or IP, port, etc...

If someone creates a new database XY, you will not be able to connect to it unless you configure it in your application. And if you do, why not where it should be, which is the database config file. You can still use the database name and config index, and use it to load the required database config, as per my previous example.

Alternatively, you can always create the config array at runtime, in your controller. Check the user guide, "manually connecting...".

Db name is only dynamic and other things are constant

Use the "manual connecting" option as described in the user guide...

Theme © iAndrew 2016 - Forum software by © MyBB