Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 11-30-2015, 07:34 PM
Julchen Julchen is offline
 
Join Date: May 2006
Posts: 11
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Connecting to a custom database using db_connect

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?

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.
Reply With Quote
  #2  
Old 11-30-2015, 11:44 PM
Dragonsys's Avatar
Dragonsys Dragonsys is offline
 
Join Date: Jan 2008
Location: DFW, Texas
Posts: 743
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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?
Reply With Quote
  #3  
Old 12-01-2015, 11:38 AM
Julchen Julchen is offline
 
Join Date: May 2006
Posts: 11
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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


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*?
Reply With Quote
  #4  
Old 12-01-2015, 01:16 PM
Dragonsys's Avatar
Dragonsys Dragonsys is offline
 
Join Date: Jan 2008
Location: DFW, Texas
Posts: 743
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #5  
Old 12-01-2015, 01:28 PM
Dave Dave is offline
 
Join Date: May 2010
Posts: 2,583
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #6  
Old 12-01-2015, 02:13 PM
squidsk's Avatar
squidsk squidsk is offline
 
Join Date: Nov 2010
Posts: 969
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
Благодарность от:
Dragonsys
  #7  
Old 12-07-2015, 01:00 AM
Julchen Julchen is offline
 
Join Date: May 2006
Posts: 11
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Quote:
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.
PHP Code:
"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
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 02:12 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.07818 seconds
  • Memory Usage 2,224KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (7)post_thanks_box
  • (1)post_thanks_box_bit
  • (7)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit
  • (7)post_thanks_postbit_info
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • fetch_musername
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • post_thanks_function_fetch_thanks_bit_start
  • post_thanks_function_show_thanks_date_start
  • post_thanks_function_show_thanks_date_end
  • post_thanks_function_fetch_thanks_bit_end
  • post_thanks_function_fetch_post_thanks_template_start
  • post_thanks_function_fetch_post_thanks_template_end
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete