OpenBroadcaster Documentation
classes / obfdb.php

OBFDB

Class

Database class. Manages SQL connections and queries, keeps track of last query and errors. Also hosts some utility methods.

public  &get_instance ( )

Create an instance of OBFDB or return the already created instance.

Returns instance
public  __construct ( )

OBFDB constructor. Adds connection information to the instance based on what's defined in OB global setting (OB_DB_HOST, OB_DB_USER, OB_DB_PASS, OB_DB_NAME), and fires a few queries to set the SQL mode.

public  add_profile ( query, duration )

Add a database profile. Returns FALSE if profiling is disabled for this instance.

Parameters
query
duration
Returns status
public  affected_rows ( )

Get the number of affected rows from the last query.

Returns num_affected
public  assoc_list ( )

Get an associative list from the previous query as an array.

Returns assoc_list
public  assoc_row ( )

Get the next associative array from the last query. Useful when there is only one presumed row (e.g. when selecting with WHERE ID = x).

Returns assoc_row
public  calc_found_rows ( )

Call this to be able to use found_rows() after the select.

public  delete ( table )

High-level delete function using parameters set by Active-Record methods. Returns the result of query() or FALSE if no WHERE is set (to avoid inadvertently deleting all rows).

Parameters
table
Returns result
public  disable_profiling ( )

Disable database profiling.

public  enable_profiling ( )

Enable database profiling. Note that this clears any pre-existing profiles set for this instance by assigning a new empty array to it.

public  error ( )

Get a string description of the last error.

Returns error
public  escape ( str )

Escape a string.

Parameters
str
Returns escaped_str
public  format_backticks ( value )

Format a value by escaping it and wrapping backticks around it.

Parameters
value
Returns formatted_value
public  format_datetime ( timestamp = null )

Generate a SQL-formatted datetime from a timestamp.

Parameters
timestamp
Returns sql_timestamp
public  format_table_column ( value )

Format a table.column value by wrapping backticks around both table and column.

Parameters
value
Returns formatted_value
public  format_value ( value )

Format a value to be used in SQL queries, escaping it and wrapping quotes around it if it's not an integer.

Parameters
value
Returns formatted_value
public  found_rows ( )

Get the number of 'found rows' (ignoring limit/offset) from the last query. Returns FALSE if no rows were found or calc_found_rows() wasn't used.

Returns num_rows
public  get ( table )

High-level get function, using previously set parameters in Active-Record. Returns associative list with results or FALSE.

Parameters
table
Returns assoc_list
public  get_one ( table )

Same as get(), but returns only the first result. Useful when selecting based on a specific ID. Returns FALSE if no row is returned.

Parameters
table
Returns assoc_list
public  get_profiles ( )

Get database profiles.

public  id_exists ( table, id )

Quick query to check if an ID exists in a table.

Parameters
table
id
public  indexed_list ( )

Get an indexed array from the last query.

Returns index_list
public  indexed_row ( )

Get the next indexed row from the last query.

Returns index_row
public  insert ( table, data )

High-level insert function using parameters set with Active-Record methods. Returns insertion ID or FALSE if the query failed.

Parameters
table
data
Returns insert_id
public  insert_id ( )

Get the ID of the last insert query.

Returns id
public  last_query ( )

Get the last query.

Returns last_query
public  leftjoin ( table, column1, column2 )

Active-Record, add LEFT JOIN to query.

Parameters
table
column1
column2
public  limit ( limit )

Active-Record, add LIMIT to query. Returns FALSE if no valid integer provided.

Parameters
limit
Returns status
public  num_rows ( )

Get the number of rows from the last select query.

Returns num_rows
public  offset ( offset )

Active-Record, add OFFSET to query. Returns FALSE if no valid integer provided.

Parameters
offset
Returns status
public  orderby ( column, dir='asc' )

Active-Record, add ORDER BY to query.

Parameters
column
dir Direction, default 'asc'.
public  orderby_string ( string )

Set a custom ORDER BY string. Is NOT escaped, so needs to be used with care.

Parameters
string
public  query ( sql )

Basic SQL query. Pass SQL to the method which will be run on the configured database.

Note that this method does NOT provide the safeties in place in query-builders. As a result, be EXTRA sure to strip any possible problematic characters from queries, and just entirely avoid using this with user-controlled input if at all possible.

Parameters
sql The SQL query to run on the database.
public  random_order ( )

Return query result in random order.

private  reset_hlvars ( )

Reset the Active-Record query.

public  update ( table, data )

High-level update function using parameters set with Active-Record methods. Returns the result from query(), or TRUE if nothing to update.

Parameters
table
data
Returns result
public  what ( column, as=null, escape=true )

Active-Record, add WHAT to query.

Parameters
column
as
escape Escape column string, default TRUE.
public  where ( column, value, operator='=' )

Active-Record, add WHERE to query.

Parameters
column
value
operator Comparison operator, default '='.
public  where_col ( column, value, operator='=' )

Equivalent to where().

Parameters
column
value
operator Comparison operator, default '='.
public  where_like ( column, value )

Active-Record, add WHERE LIKE to query.

Parameters
column
value
public  where_mode ( mode )

Set the WHERE mode for the query to OR or AND. Returns FALSE when an invalid mode is selected.

Parameters
mode
Returns status
public  where_not_like ( column, value )

Active-Record, add WHERE NOT LIKE to query.

Parameters
column
value
public  where_string ( string )

Set a custom WHERE string. Is NOT escaped, so needs to be used with care.

Parameters
string