vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB5 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=263)
-   -   Accessing DB in another script (https://vborg.vbsupport.ru/showthread.php?t=323712)

gambler726 10-27-2016 03:50 PM

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!

Dave 10-27-2016 06:43 PM

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.

TheLastSuperman 10-27-2016 07:10 PM

Quote:

Originally Posted by Dave (Post 2577365)
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! :D

noypiscripter 10-28-2016 08:12 PM

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.


All times are GMT. The time now is 01:57 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.01341 seconds
  • Memory Usage 1,745KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_code_printable
  • (3)bbcode_php_printable
  • (2)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (4)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete