MyBB Community Forums

Full Version: Database Querying issue.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all, 

I'm currently working on an external page for MyBB to use within a community I associate with. The aim is to query the database and create a "family tree" based on the results. Previously, I had a working version of this, however I recently decided an update would be required for continued use. This is where I hit a stone wall. As it currently stands, I cannot seem to get my SQL command to work, and when it does, it produces some weird results to say the least. 

Here is a screenshot of what occurs when I get a basic query to work: 
https://gyazo.com/36bf144e9734367a12214faa1f6b8169

And this is the attached code required to produce this: 
function orbatDisplay($usersInGroup, $groupID)  {  
global $db;
    $query = $db->write_query("SELECT mybb_users.uid, mybb_users.username, mybb_users.usergroup, mybb_users.additionalgroups, mybb_usergroups.gid, mybb_usergroups.description FROM mybb_users, mybb_usergroups");
    while($result = $db->fetch_array($query)){
        $username = $result['username'];
        $uid = $result['uid'];
        $addGroups = $result['additionalgroups'];
        print '<div class="name">'. $username .' '. $uid .' </div>';
    }
    for($x = 0; $x < $usersInGroup; $x++) {
        print '<div class="name"><a href="#">Closed</a></div>';   
    }
}

Now, I'm aware that the additional 6 "closed" lines in the front end is a result of poor logic here, as the original version made use of a foreach loop, instead of while. 

Now, this is fine, but I'm confused as to why it is repeating the same singular record in the table (there is only one registered user). 

However, when I introduce a more complicated query, nothing is returned. The rest of the function is the same, only $query changes:

SELECT mybb_users.uid, mybb_users.username, mybb_users.additionalgroups, mybb_usergroups.gid, mybb_usergroups.description FROM mybb_users, mybb_usergroups WHERE mybb_users.usergroup=mybb_usergroups.gid AND CONCAT(",",mybb_users.additionalgroups,",") LIKE '%,'.$groupID.',%' ORDER BY mybb_usergroups.gid DESC

I'm confused as to why this doesn't return any results. Even when I substitute '. $groupdID .' for a real value (4 for testing purposes), no values are returned. I would appreciate any help in solving this. For reference, here is the php elements I am using to link to myBB: 

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

Any help is greatly appreciated!

Thanks,
Flinty

Update. I have managed to get something working, however my issue now is being unable to figure out how to bind parameters in the myBB system. Code is as displayed below: 

    global $db;
    $stmt = $db->query('
        SELECT mybb_users.uid, mybb_users.username, mybb_users.additionalgroups, mybb_usergroups.gid, mybb_usergroups.title 
        FROM mybb_users, mybb_usergroups 
        WHERE mybb_users.usergroup=mybb_usergroups.gid
        AND CONCAT(",",mybb_users.additionalgroups,",") 
        LIKE "%,9,%"
        ORDER BY mybb_usergroups.gid DESC
    ');
    $total = $stmt->num_rows;
    // print $total;
    foreach ($stmt as $row)
    {
        print '<div class="name"><a href="forums/member.php?action=profile&uid='. $row['uid'] .'">' . $row['title'] . '. ' . $row['username'] . '</a></div>'; 
    }
    for($x = 0; $x < $usersInGroup-$total; $x++)
    {
        print '<div class="name"><a href="#">Closed</a></div>';
    }

My issue is not being able to bind the parameter "9" to $groupID defined in the function call. 

Does anyone know how to bind variables for MyBB queries? 

Thanks