MyBB Community Forums

Full Version: [Solved] MyBB's MySQL Functions?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

Just a quick question, does MyBB have it's own functions for connecting to it's database and getting data? I'm making a ban list for my forum and I'd prefer to be consistent in my coding, instead of using the standard PHP MySQL functions.

Basically, what are MyBB's mysql functions (if there are any), and could you give me some examples with them?

Also, I've had a look at a tutorial on making custom pages, but how can I execute a query from that? Would I define everything in the php file then call it in the template with {$var_name}?

Thanks Big Grin
If you have already "require './global.php';" then you can use MyBB's database functions. When you require global.php MyBB will automatically connect to the database, so you don't need to connect again. The database object is $db, and the most simple method is $db->query("your query");. This method wraps mysql_query. The other common ones are:

$db->fetch_array( query ) -- mysql_fetch_array
$db->num_rows( query ) -- mysql_num_rows
$db->escape_string( string ) -- mysql_real_escape_string
$db->insert_id() -- mysql_insert_id
$db->error() -- mysql_error (note this method will be renamed in MyBB 1.4)

The above are used the same way as their standard PHP functions. You can see the full list of methods defined in inc/db_mysql.php.

In addition to wrapping commonly used standard PHP methods for MySQL, we also have our own "simple" methods for insert, update, select, and delete queries.

$db->simple_select( string $table_name [, string $what_to_select [, string $where [, array $options]]] )
example:
$db->simple_select(TABLE_PREFIX."users", "uid, username, email", "usergroup=4", array("order_by" => "uid", "order_dir" => "desc", "limit_start" => 0, "limit" => 10));
The above is equivalent to:
$db->query("SELECT uid, username, email FROM ".TABLE_PREFIX."users WHERE usergroup=4 ORDER BY uid DESC LIMIT 0, 10");

$db->insert_query( string $table_name, array $information )
example:
$insert = array("uid" => 1, "fid" => 4);
$db->insert_query(TABLE_PREFIX."forumsubscriptions", $insert);
The above is equivalent to:
$db->query("INSERT INTO ".TABLE_PREFIX."forumsubscriptions (uid, fid) VALUES (1, 4)");

$db->update_query( string $table_name, array $information [, string $where [, int $limit]] )
example:
$update = array("usergroup" => 4);
$db->update_query(TABLE_PREFIX."users", $update, "uid=1");
The above is equivalent to:
$db->query("UPDATE ".TABLE_PREFIX."users SET usergroup=4 WHERE uid=1");

[b]$db->delete_query( string $table_name [, string $where [, int $limit]] )[/b]
example:[php]$db->delete_query(TABLE_PREFIX."posts", "uid=2");
The above is equivalent to:
$db->query("DELETE FROM ".TABLE_PREFIX."posts WHERE uid=2");


If you want to execute a query from a custom page, just put the query in the PHP file, and whatever you want to output you make into a variable and then put that in the template.
$query = $db->simple_select(TABLE_PREFIX."users", *", "", array("order_by" => "RAND()", "limit" => 1));
$rand_user = $db->fetch_array($query);
$rand_user_username = $rand_user['username'];
Then in your template you would use:
Random user: {$rand_user_username}

Hope that helps.
DennisTT Wrote:Hope that helps.

That more than helped! You truely are a credit to the project!

Archived Smile
Yeah I'm going to post that on the wiki later also.
Back again Smile

This is what I have so far: http://interwebz.net/showbans.php

However, there are two bans in the banned table. How can I get each ban shown? Would it be some kind of "for" loop?

Edit:

Ulp, nevermind.

http://uk.php.net/while beat ya to it Big Grin
All done for now:

http://interwebz.net/showbans.php

If anyone would like it (or if someone wants to make it a plugin), get in contact!
I looked at one of Chris Boulton's plugins, and learnt from that.

http://mods.mybboard.net/view.php?did=609 is the finished product Smile

Again, thanks for the help DennisTT, it was a brilliant learning experience!