#1 September 28, 2026 1:47pm

doon.mok
Member
Registered: May 6, 2015
Posts: 149

Connect to external DB through module

Is it possible to connect to an external database on the back end of a module?
I added the database credentials through environment.php

// External Database
    $bigtree["config"]["ex"]["host"] = "";
    $bigtree["config"]["ex"]["name"] = "";
    $bigtree["config"]["ex"]["user"] = "";
    $bigtree["config"]["ex"]["password"] = "";
    $bigtree["config"]["ex"]["port"] = "3306";
    $bigtree["config"]["ex"]["socket"] = "";

custom/inc/modules/connect.php,
I want to connect to an external database and tried connecting in "public function __construct()".
When I try to pull from the external table, I get the error
Fatal error: Uncaught mysqli_sql_exception: Table 'localhost.audience' doesn't exist

Its still looking at localhost database.

Offline

#2 September 28, 2026 1:53pm

timbuckingham
Administrator
From: Baltimore, MD
Registered: April 2, 2012
Posts: 983

Re: Connect to external DB through module

You can't use the built in SQL helper functions to access a separate database but you should be able to create a MySQLi connection yourself and access it that way.

e.g.

$db = new mysqli($bigtree["config"]["ex"]["host"], $bigtree["config"]["ex"]["user"],  $bigtree["config"]["ex"]["password"], $bigtree["config"]["ex"]["name"]);

$query = $db->query("SELECT * FROM whatever");

while ($row = $query->fetch_assoc()) {
   /// do something;
}

Offline

Board footer

Powered by FluxBB

The Discussion Forum is not available on displays of this size.