View Full Version : Accessing DB in another script
gambler726
10-27-2016, 03:50 PM
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...
$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: http://www.mysite.com/forum/includes/
Thanks!
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:
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_ERRMODE, PDO::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
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:
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_ERRMODE, PDO::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.
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/david-grove/4327147-how-to-log-into-vbulletin-from-an-external-script-on-your-website
It calls the user API to login and did not have to manually connect to vB database.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.