Go Back   vb.org Archive > vBulletin 5 Connect Discussion > vB5 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 10-27-2016, 03:50 PM
gambler726 gambler726 is offline
 
Join Date: May 2016
Posts: 8
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Accessing DB in another script

I know little about writing VB scrips / mods, but I am trying to use the VB config.php to access the DB from another part of my site.

With the DB settings set to this in config.php...

PHP Code:
$config['MasterServer']['servername'] = 'myserver';
$config['MasterServer']['port'] = 3306;
$config['MasterServer']['username'] = 'myusername';
$config['MasterServer']['password'] = 'mypassword'
... can someone please help me write the connect code?

Let's say the config.sys file is in:
Code:
http://www.mysite.com/forum/includes/
Thanks!
Reply With Quote
  #2  
Old 10-27-2016, 06:43 PM
Dave Dave is offline
 
Join Date: May 2010
Posts: 2,583
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

It depends on where you place your script.
If your script is in the root and your forum is located in /forum/ then you can connect doing something like:

PHP Code:
require("forum/includes/config.php");
$dbname "somedatabasename";

try {
    
$db = new PDO("mysql:dbname=" $dbname ";host=" $config['MasterServer']['servername'] . ";port=" $config['MasterServer']['port'], $config['MasterServer']['username'], $config['MasterServer']['password']);
    
$db->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
} catch(
PDOException $e) {
    die(
'Could not connect to the database:<br/>' $e);
}

$stmt $db->prepare("SELECT mycolumn FROM sometable WHERE somevalue = ? AND somevalue2 = ?");
$stmt->execute(array("somevalue1""somevalue2"));

// Iterate through data
foreach($stmt as $row){
   
var_dump($row); // $row['mycolumn']
}

// Or if only one row result
$row $stmt->fetch();
var_dump($row); // $row['mycolumn'] 
* Quickly wrote it without testing.
Reply With Quote
Благодарность от:
gambler726
  #3  
Old 10-27-2016, 07:10 PM
TheLastSuperman's Avatar
TheLastSuperman TheLastSuperman is offline
Senior Member
 
Join Date: Sep 2008
Location: North Carolina
Posts: 5,844
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Dave View Post
It depends on where you place your script.
If your script is in the root and your forum is located in /forum/ then you can connect doing something like:

PHP Code:
require("forum/includes/config.php");
$dbname "somedatabasename";

try {
    
$db = new PDO("mysql:dbname=" $dbname ";host=" $config['MasterServer']['servername'] . ";port=" $config['MasterServer']['port'], $config['MasterServer']['username'], $config['MasterServer']['password']);
    
$db->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
} catch(
PDOException $e) {
    die(
'Could not connect to the database:<br/>' $e);
}

$stmt $db->prepare("SELECT mycolumn FROM sometable WHERE somevalue = ? AND somevalue2 = ?");
$stmt->execute(array("somevalue1""somevalue2"));

// Iterate through data
foreach($stmt as $row){
   
var_dump($row); // $row['mycolumn']
}

// Or if only one row result
$row $stmt->fetch();
var_dump($row); // $row['mycolumn'] 
* Quickly wrote it without testing.
Quote:
You must 'Like' someone else's post before liking any more by Dave.
+1!
Reply With Quote
  #4  
Old 10-28-2016, 08:12 PM
noypiscripter's Avatar
noypiscripter noypiscripter is offline
 
Join Date: Jul 2013
Posts: 468
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Based on what you want to do, you don't need to manually connect to the database but simply call the vB API externally from a different site that's on the same server.

Take a look at this blog on how to login to vB from an external site.

http://www.vbulletin.com/forum/blogs...n-your-website

It calls the user API to login and did not have to manually connect to vB database.
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 01:41 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.03885 seconds
  • Memory Usage 2,216KB
  • Queries Executed 11 (?)
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_code
  • (3)bbcode_php
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (4)post_thanks_box
  • (1)post_thanks_box_bit
  • (4)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit
  • (4)post_thanks_postbit_info
  • (4)postbit
  • (4)postbit_onlinestatus
  • (4)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_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