CodeIgniter Forums
Best way to get just a tiny bit of data from another database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Best way to get just a tiny bit of data from another database (/showthread.php?tid=9635)



Best way to get just a tiny bit of data from another database - El Forum - 07-02-2008

[eluser]Bramme[/eluser]
Hey all,

I'm working on a new website (my second in CI ^^), but I've stumbled upon a problem:

The site has a phpbb3 forum and people who comment on the site announcements or blog entries can enter their forums password, that way their avatar gets shown etc.

This is the only time I need to connect to another database. What would be a good coding approach to this?

Thanks for your thoughts and time!


Best way to get just a tiny bit of data from another database - El Forum - 07-03-2008

[eluser]Bramme[/eluser]
Okay, after reading the user guide, I decided to use this approach to connect to the phpbb3 database:

Code:
$dsn = 'mysql://username:password@localhost/db';
$phpbb = $this->load->database($dsn, true);
then I can use it in my code snippet. However, I would like to disconnect from the database when I've finished with it, seems like unnecessary resources to me to keep a second db connection open. But I couldn't find this in the user guide, so my final question is: how would I disconnect from $phpbb then? Just use mysql_close() ?


Best way to get just a tiny bit of data from another database - El Forum - 07-03-2008

[eluser]Jeff Logan[/eluser]
You can use mysql_close(), although non-persistent open links are automatically closed at the end of the script's execution so it is not necessary (although good coding practice - expecially if you are using a mixture of non-persistent and persistent connections in your code).

You can also specify which connection to close, otherwise it closes the last connection made.

Code:
mysql_close ([ resource $link_identifier ] )

Hope the above helps.


Best way to get just a tiny bit of data from another database - El Forum - 07-03-2008

[eluser]Bramme[/eluser]
Yeah, that's directly from the php.net documentation, but how do I get $link_identifier? It can't be just $phpbb (as in my example) seeing as that's an object...

Meh, I'll go through the library and hope to find it.


Best way to get just a tiny bit of data from another database - El Forum - 07-15-2008

[eluser]elvix[/eluser]
I think the way to go (least how I've got it working) for a one-time call to a second db is:

Set up second DB config array in database config file.

$config['newdb']['hostname'] ....

Set pconnect to FALSE

Create object for other DB: $NEWDB = $this->load->database('newdb');

Now you can grab something from your other DB and CI will go back to your primary DB when you're done.

See manual: database config, connecting to multiple databases for more info.