Game-server MySQL
#1
I'm not sure if this is the right place, I'll just give it a shot though.
I have a MyBB forum set-up with a MySQL database connected with the website control panel. However, I'd like to know if it's possible to somehow connect another MySQL database to my website. It's from my game server, and I'd like to post statistics of the players, their currency, experience and so on. Is this possible in any way?

I figured that it is possible, however I'd like to know how I can connect to the secondary game database on the /index.php page.

I've finally managed to connect to the game server database. However I'm struggling with the placement on the index page. How do I save the functions like this:

//execute the SQL query and return records
$result = mysql_query("SELECT steamid, balance FROM uconomy LIMIT 3");

//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
echo "ID:".$row{'steamid'}." Name:".$row{'balance'}."Year: "."<br>"; //stats
}

into a $uconomy function to be posted on the html template?
Reply
#2
The best way is to create a plugin file rather than directly edit index.php because otherwise anytime a new version is released you'd need to do your customizations again. For some instructions on creating a plugin, you can view this thread: http://community.mybb.com/thread-137925.html

Create a file in your inc/plugins directory and hook to index_end. Then create your function.

function myfunction()
{
global $db, $templates, $myvariable;
$query = $db->query("/*Your query here */");
while($result = $db->fetch_array($query))
{
$myvariable .= "ID: " . $result['steamid'] . " Name: " . $result['balance'] . " Year: " . $result['year'] . "<br />";
}
}

Now you can use $myvariable somewhere in your index template.
Reply
#3
(2015-09-11, 06:45 PM)dragonexpert Wrote: The best way is to create a plugin file rather than directly edit index.php because otherwise anytime a new version is released you'd need to do your customizations again.  For some instructions on creating a plugin, you can view this thread: http://community.mybb.com/thread-137925.html

Create a file in your inc/plugins directory and hook to index_end.  Then create your function.

function myfunction()
{
global $db, $templates, $myvariable;
$query = $db->query("/*Your query here */");
while($result = $db->fetch_array($query))
{
$myvariable .= "ID: " . $result['steamid'] . " Name: " . $result['balance'] . " Year: " . $result['year'] . "<br />";
}
}

Now you can use $myvariable somewhere in your index template.

Alright. Is it possible to connect to a secondary database, that has nothing to do with the database that MyBB is intalled into, in the pluginfile.php? I just want it to read from it. I'm not sure but, global $db is the primary database that MyBB is installed into. Right?
Reply
#4
There is a database method to connect to a different database. You should be able to use $db->connect like this, I haven't tried it in a while though so you may have to play around a bit.

The relevant code on Github can be found here: https://github.com/mybb/mybb/blob/featur...l.php#L146


$db->connect(array("hostname" => "localhost", "username"=> "your mysql user", "password"=> "your mysql user password", "database"=> "your database name"));
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)