Two Databases, One config.php?

Two Databases, One config.php?

th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

I've read some old posts here about setting up two databases in one config.php file. Both databases have different user/password credentials. Is it still not possible to make more than one connection to a database in Datatables? Is there a way to work around this?

This question has accepted answers - jump to:

Answers

  • th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

    I forgot some info... I have two applications that use two separate databases. Each database has it's own username/password. The directory where editor/Datatables resides is a symbolic link in both website directories thus they share the same config.php. Is it possible to put two "$sql_details arrays in config.php?

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    You can make as many connections to different databases as you want. The config.php file does an "auto-magic" connection using the details provided and assigns it to the $db variable. But you can also create connections using new DataTables\Database( ... ) where the ... are the database credentials - e.g.:

    $db2 = new DataTable\Database([
      'user' => 'myUser',
      'pass' => 'myPass',
      // etc
    ]);
    

    You can then use your new connection as needed.

    Regards,
    Allan

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    For info, the "auto-magic" connection is created in Bootstrap.php.

    Allan

  • th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

    Thank you Allan. I haven't been on the site for a couple years.
    So I added a new $db2 in the Bootstrap.php file and a new $sql_details2 in config.php. So far it seems to work fine. Thank you for the info.
    Would I be able to call $db and $sql_details anything I'd like? Are they used elsewhere other than in my code?

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Yes, use it anywhere you want. Generally I would suggest not modifying the library files, so when you come to upgrade, you don't need to modify them again.

    Allan

  • th3t1ckth3t1ck Posts: 228Questions: 37Answers: 1

    Ok. Good idea! I'm not doing any server-side stuff yet and I didn't want the database info, user/password, etc., viewable in the code. I'm just working on a couple projects for myself locally, no internet access.
    I'll definitely keep that in mind however. I'm sure I would forget I did that and it would be simpler when I want to use more databases for other applications.

Sign In or Register to comment.