The following warnings occurred:
Warning [2] Undefined variable $unreadreports - Line: 26 - File: global.php(961) : eval()'d code PHP 8.2.25 (Linux)
File Line Function
/global.php(961) : eval()'d code 26 errorHandler->error
/global.php 961 eval
/printthread.php 16 require_once



UserSpice
Multiple Database Connections - Printable Version

+- UserSpice (https://userspice.com/forums)
+-- Forum: Support Center (https://userspice.com/forums/forumdisplay.php?fid=23)
+--- Forum: UserSpice 4.3 and Below (https://userspice.com/forums/forumdisplay.php?fid=26)
+--- Thread: Multiple Database Connections (/showthread.php?tid=1132)



Multiple Database Connections - usamusa - 09-03-2018

Hello,

is there a way to allow users to connect to multiple Databases using the same DB class?

I hope I'd be able to do something like:
Code:
$db1->findAll('TableInDB1');
$db2->findAll('TableInDB2');


Thank you very much


RE: Multiple Database Connections - mudmin - 09-03-2018

Yes.  So if both dbs use the same credentials (as in same username, same host, same password), you can just do 
$db1 = DB::getDB(['db1name']);
$db2 = DB::getDB(['db2name']);

Then use them as you would the regular db.  If you have a different configuration, let me know and we can figure it out.  

Note that this feature is part of 4.4 and I'm not 100% sure if I pushed it out on the latest 4.3 release, so if that doesn't work, just replace users/classes/DB.php with https://pastebin.com/enb0zaGt


RE: Multiple Database Connections - usamusa - 09-03-2018

Thank you for the quick reply!

Same credentials certainly works.

It would be awesome though, if i could allow every user access to a table in another DB using different credentials.
Would that be possible in 4.4?

Thank you very much


RE: Multiple Database Connections - mudmin - 09-03-2018

I'm working on that. Right now, there's a way where you can put the credentials in the init.php, so you can do

$GLOBALS['config'] = array(
'mysql' => array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'db' => 'oe1',
),
'myotherdb' => array(
'host' => '192.168.25.16',
'username' => 'bob',
'password' => 'bobspw',
'db' => 'oe2',
),

And then do $db2 = getDB(['myotherdb','ns']); //ns means new server
But I haven't written the ability to specify credentials on the fly. I'll work on that.


RE: Multiple Database Connections - usamusa - 09-04-2018

Thanks! I really appreciate it.