MyBB Community Forums
Database queries. - Printable Version

+- MyBB Community Forums (
+-- Forum: 1.8 Support (
+--- Forum: General Support (
+--- Thread: Database queries. (/thread-210145.html)

Pages: 1 2

Database queries. - Freakout14 - 2017-05-01

Hey guys.

I have been using MyBB a lot since the last two years but I haven't yet done anything with Database but today I need to. I'm clueless here and would love some assistance.

The thing is I need to pull some data from a TABLE that is in the same database as my FORUM.

I need to display all that data later in user profiles. So as far as my guess goes I'll have to make a query in the member.php file.

If anyone could provide me with some help or example I'll be eternally gratefully.

Cheers and regards.

RE: Database queries. - .m. - 2017-05-01

can you tell us what exactly you want to get from the database ..

RE: Database queries. - Freakout14 - 2017-05-01

(2017-05-01, 11:06 AM).m. Wrote: can you tell us what exactly you want to get from the database ..

Just a few records to display that are stored in another table. I have created a new page to display the data. So I should add the queries in the PHP file of the new page right?

Can you provide me with an example query. I would like to fetch data from a table that is in the same database as my forum one.

RE: Database queries. - .m. - 2017-05-01

here is a simple example php file to get user groups names & their IDs

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

	$query = $db->simple_select("usergroups", "*", "");
	$sep = "";
	while($group = $db->fetch_array($query))
		$groupname = $group['title'];
		// GID
		$gid = $group['gid'];
		$groups .= "{$sep} [ {$gid} ] &rarr; {$groupname}";
		$sep = "<br />";

echo $groups;

see Database Methods guidance

RE: Database queries. - Freakout14 - 2017-05-01

^ Thanks for that not exactly what I wanted but it does help.

Another question suppose I have a query. How will I save the output of that query into a global variable and use it wherever I require?

RE: Database queries. - .m. - 2017-05-01

it would be better to create a plugin & make required variable global by using a global hook.

* plugin basics | * database methods | * plugin hooks
* guidance on plugins making | * plugins & hooks explained | * Plugin Authoring For Beginners
* How to make a plugin | * quick tutorial on plugins making
* advanced plugin development

RE: Database queries. - Freakout14 - 2017-05-01

^ No that isn't gonna help sorry. Making a plugin just to pull a few records is way too much work.

I'm able to pull data now but I just don't know how to store the data of the query in a variable and use it on the same page.

RE: Database queries. - .m. - 2017-05-01

your file need not be a pure plugin but plugin hook needs to be used & file needs to be added to plugins folder

though made for a different purpose, see attachment here for the display method !

RE: Database queries. - Freakout14 - 2017-05-01

$query = $db->query("SELECT username from housing");
	while($result = $db->fetch_array($query))
        $housingvar .= "Adress: " . $result["Adress"]. ;
		return $housingvar;


I'm using the variable $housingvar in the template but no return value. However this does seem to work if I use echo.

RE: Database queries. - broatcast - 2017-05-01

you need to eval this variable to use it in the template system