MyBB Community Forums

Full Version: Need help with pulling info out of the DB
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I have designed my forums and spent a lot of time in it, I am just wondering (because im running games) how I could go about adding in a feature that pulls out database info?

So far I've hopefully successfully added the database category under mybb_servers, id appreciate anyone that corrects it if its wrong but I just want to store on or off in 'status'. This will be used later in an IF statement.

[Image: 7c287d616092499cb9e586606f8e8d78.png]
[Image: 4ad7cb37b4ca02bc0e2b71af72e7835b.png]

however, what i want to do is pull whats stored in 'status' and put in on my forums. So if its a 'yes' it will display 'online' and no, 'offline'. The IF statement is easy, i just need some help on checking if my database is set-up right and the code to retrieve the content inside of it.

So far I've tried alot and researched but got this far:
[Image: e825aec6fe62770f8b4da097635d1a75.png]

which gives this error:
[Image: f3931d132fcd0f3345000b8d352490f4.png]

Thank you in advance!

P.s On line 116 is just a ending php statement like that:      ?>
well this is f'ing bullshit, so much for support... f you all
check modified code in index template - that should have a clue for the error.
Look, i just need someone with PHP knowledge of extracting information held in a database to store in a variable for later use... an example would be good enough... I didn't ask to play find the clues, this is my last hope and its starting to piss me off that the support is so bad here.. I might jus change software.
well, you can change software to whatever you like. anyway, are you using php code directly in templates ?

php code cannot be used directly in MyBB templates. it requires a plugin (see related plugin)
moreover using php in templates to fetch content from database is not correct method.
its better to use separate php file for your requirement.
Using language such as the language you used above isn't going to help you. It'll drive people away from helping you if anything at all. You waited less than a day before exploding in a fit of bad language, showing an incredible lack of patience.

Anyway, regarding your actual issue, where have you included the code you posted. Is it in a template or a separate PHP file, or a plugin? The problem is that your $query variable appears to be an object (of type mysql_result), which you're then trying to [pre]echo[/pre]. This will obviously not work.

To retrieve a set of results from the database, loop through them and echo the "status" values, use something like the following:

<?php

require_once__DIR__ . '/global.php';

$query = $db->simple_select('mybb_servers', '*', "status = 'ono'");

while ($result = $db->fetch_array($query)) {
    echo $result['status'];
}

This makes use of the database class' "simple_select" function, which will build your SQL query for you. It then retrieves each row as an associative array (see the while loop).

I'd definitely recommend looking at the code for both some plugins and the MyBB core. There are plenty of examples both simple and advanced of using the database classes and methods.
Thankyou very much!

I have changed the code a bit and managed to get it working inside a php file which i will then just use Smile
<?php

	define('IN_MYBB', 1);
	require_once "./global.php";

$query = $db->simple_select('servers', '*', "status = 'off'");

while ($result = $db->fetch_array($query)) {
    if ($result['status'] !="") {
echo "Offline";
} else {
echo "Online";
}
}

?>