PDA

View Full Version : Connecting to a custom database using db_connect


Julchen
11-30-2015, 07:34 PM
Is there a sane way to access a custom database using vB's built in database classes?
The database is on the same local server as the vB one.
Is there a recommended strategy on using db_connect and select_db? Like opening a second connection to the same server or just switching databases on the same connection?
Is it even a sane idea to crowbar in a 2nd database that way? :o

Over the years I created quite a few extra pages and stuff but so far I've been using completly "seperate" code to make them run.
Since I'm in the process of migrating my 3.8 install to 4.2, though, I need to rewrite everything anyway and I'd love to, finally, create proper installable addons/plugins for everything.

Yet, all the data is in the custom database, and I don't feel comfortable merging it with the forum tables.

Dragonsys
11-30-2015, 11:44 PM
You would be better off merging the 2 databases, and using table prefixes to keep them visually separated; however, if you are re-writing it for v4, then just take your time and write them properly using the same DB with whatever prefix VB is using,

Why are you not comfortable doing it this way?

Julchen
12-01-2015, 11:38 AM
Primarily because of backup reason.
Some of my apps, especially the largest one, don't necessarily need the vB environment and would do just as well as "standalones".
Right now, when I want those tables backed up and/or transfered to a test server I just blindly mysqldump the whole secondary database. Same with restoring.
After merging I'd need to specifically pick or exclude tables (forum takes about 500MB, 2nd db only 20-30MB). Knowing myself, this will end in confusion :D


Would switching between databases cause any kind of trouble with the forum?
Like ... my script switches to db2, forum script tries to access its own tables and ... *bam*?

Dragonsys
12-01-2015, 01:16 PM
No, it should work, but I'm not sure you will be able to use VB's db_query, you will probably have to make your own, for calls to your other DB.

Dave
12-01-2015, 01:28 PM
You could approach this situation in 2 different ways:
First way is to create an API for your apps which removes the need of database access inside of vBulletin.
Second way is to initialize a new vB_Database class (refer to includes/class_core.php) with your app its database connection information. That way it should not interfere with vBulletin.

squidsk
12-01-2015, 02:13 PM
To use the existing vb structure to access the database just create a new instance of vbDatabaseMySQLi class and then call the connect method with the relevant information. See the file includes/class_core.php for details on what you need to do the above. After that you can use the standard db->query_read, db->query_write, etc. function calls.

Julchen
12-07-2015, 01:00 AM
Ok, so I did some experiments.

Using vB's existing connection and switching over to a custom database works ... kinda.
The problem indeed is this
my script switches to db2, forum script tries to access its own tables and ... *bam*
vB does not automatically switch back to its own configured database. Dang!


What works fine now, though, is including the database in the sql query itself. e.g.
"SELECT * FROM ".$dbprefix."mytable"
No switching necessary.

So basically a prefix did the trick. And I get to keep both DBs seperate, too.
Almost as good as eating cake :D