CodeIgniter Forums
[SOLVED - ]Database Forge - How to select a DB - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: [SOLVED - ]Database Forge - How to select a DB (/showthread.php?tid=75046)



[SOLVED - ]Database Forge - How to select a DB - reszko - 12-12-2019

Hi.

When I use $forge->createDatabase('databaseName'); to create a database, it works successfully, but, when I use $forge->createTable('tableName') it adds this table to my database that is configured in /app/config/database.php.

How can I work with the recent created database?

I tried to find a $forge->selectDB() method or something like this but I didn't get success.

Thank you.
My english is a work in progress.
Fabio


[SOLVED] - RE: Database Forge - How to select a DB - reszko - 12-14-2019

(12-12-2019, 04:30 PM)reszko Wrote: Hi.

When I use $forge->createDatabase('databaseName'); to create a database, it works successfully, but, when I use $forge->createTable('tableName') it adds this table to my database that is configured in /app/config/database.php.

How can I work with the recent created database?

I tried to find a $forge->selectDB() method or something like this but I didn't get success.

Thank you.
My english is a work in progress.
Fabio

Well, I don't know if this is the correct way, but I found a way to select the database I want to use.
You need to get the db connection instance, like this:

PHP Code:
$db db_connect();
//Then you do:
$db->setDatabase('newDatabase'); 

The setDatabase method uses the current database connection configs but changes only the database name.
If you need to connect to another database address, then you should create a new database group inside config/Database.php

And after this, all database forge commands will affect the new database selected.
It worked for me.

I would appreciete if someone tell me if I am doing something wrong.

Thank you all.
Fabio


RE: [SOLVED - ]Database Forge - How to select a DB - kilishan - 12-15-2019

You're doing it correctly.