MyBB Documentation

Database Methods

The 1.6 Documentation is no longer maintained and some information may be outdated.

The MyBB 1.6 series reached end of life on October 1, 2015.

This means there will be no more security or maintenance releases for these series and forums running these versions of MyBB may be at risk of unfixed security issues. All administrators are strongly encouraged to upgrade their forums to the latest release of MyBB as soon as possible.

The database class provides a wrapper of most common functions used for database access, and also others which simplify common queries. All queries performed with the database through the database abstraction layer can be analyzed for the execution time and further optimization.

Contents

Commonly Used

$db->query

Executes an SQL query with your database. Notice that the write_query method is now preferred.

$db->write_query

Similar to the query method, except write_query executes the query on the slave database in case you happen to have a multi-database server setup. Write_query is now preferred over query. For more information, see the following post from Pirata Nervo: https://community.mybb.com/post-559029.html#pid559029

$db->simple_select

See Simple Select.

$db->fetch_array

Returns an array of values for the first row (can be iterated through with a while statement)

Example:

$query=$db->query("SELECT * FROM Table [WHERE Field='Value']");
while($result=$db->fetch_array($query))
{
   $data1=$result['FieldName1'];
   $data2=$result['FieldName2'];
  (etc.)
}

$db->fetch_field

See Fetch_Field.

$db->num_rows

Returns the number of rows in the query

$db->insert_id

Returns the insert id (The id of the primary key) of the insert query just run

$db->insert_query

Runs an insert query on a table in a database. Receive two parameters:

  1. string - The table name to perform the query on.
  2. array - An array of fields and their values.

Build an insert query from the array and executes the query using $db->query

$db->update_query

Runs an update query on a table in a database.
parameters: ('table name', 'update array', 'conditional')

$db->delete_query

Used to perform a delete query on a table in a database

$db->escape_string

Replaces addslashes, escapes data before being used in a query


Others

$db->connect

Connects a new database.

$db->select_db

Selects the database for the current MySQL Session

$db->explain_query

Helps to explain queries run from on database from the current session for debugging purposes.

$db->data_seek

Moves the internal pointer to the specified column

$db->close

Closes the connection to the currently open database

$db->error_number

Returns the error number (if any) of the specified query resource

$db->error

Returns the error string (if any) of the specified query resource

$db->dberror

Outputs an error message (if any) of the specified query resource

$db->affected_rows

Returns the amount of affected rows from a "write" query

$db->num_fields

Returns the number of fields of the specified query resource

$db->list_tables

Returns the tables in the current open database

$db->table_exists

Returns true if the specified table exists

$db->field_exists

Returns true if the specified field exists

$db->shutdown_query

Runs a query thats performed when php is done parsing the file

$db->select_query

Runs a complex query (join queries). (Note: Depreciated in MyBB 1.4)

$db->get_version

Returns the version number of the database server being used

$db->optimize_table

Runs an optimize query on a table

$db->analyze_table

Runs an analyze query on a table

$db->show_create_table

Return the "create table" command for a specific table.

$db->show_fields_from

Show the "show fields from" command for a specific table.

$db->is_fulltext

Returns whether or not the table contains a fulltext index.

$db->supports_fulltext

Returns whether or not this database engine supports fulltext indexing.

$db->supports_fulltext_boolean

Returns whether or not this database engine supports boolean fulltext matching.

$db->create_fulltext_index

Creates a fulltext index on the specified column in the specified table with optional index name.

$db->drop_index

Drop an index with the specified name from the specified table



MyBB Documentation
Front End Index Page - Portal - Forum Display - Thread Display - Posting Threads and Replies - Searching - User CP - Member Profile - Private Messages - Calendar - Member List - Help Docs - Who Is Online - Lite (Archive) Mode - Forum Team - Statistics - RSS Syndication - Miscellaneous
Moderation Standard Thread Moderation - Inline Thread Moderation - Inline Post Moderation - Warning System
Mod CP Home - Forums and Posts - Users
Admin CP Home - Configuration - Forums & Posts - Users & Groups - Templates & Style - Tools & Maintenance
Templates & Themes Images - Postbit - Templates - Themes
Developer Database Methods - Database Tables
Miscellaneous Cookies - UTF8 Setup - Default Settings

Edit this page on GitHub