DB Driver Reference

This is the platform-independent base DB implementation class. This class will not be called directly. Rather, the adapter class for the specific database will extend and instantiate it.

The how-to material for this has been split over several articles. This article is intended to be a reference for them.

Penting

Not all methods are supported by all database drivers, some of them may fail (and return FALSE) if the underlying driver does not support them.

class CI_DB_driver
initialize()
Kembali:TRUE on success, FALSE on failure
Return type:bool

Initialize database settings, establish a connection to the database.

db_connect($persistent = TRUE)
Parameter:
  • $persistent (bool) – Whether to establish a persistent connection or a regular one
Kembali:

Database connection resource/object or FALSE on failure

Return type:

mixed

Establish a connection with the database.

Catatan

The returned value depends on the underlying driver in use. For example, a mysqli instance will be returned with the ‘mysqli’ driver.

db_pconnect()
Kembali:Database connection resource/object or FALSE on failure
Return type:mixed

Establish a persistent connection with the database.

Catatan

This method is just an alias for db_connect(TRUE).

reconnect()
Kembali:TRUE on success, FALSE on failure
Return type:bool

Keep / reestablish the database connection if no queries have been sent for a length of time exceeding the server’s idle timeout.

db_select([$database = ''])
Parameter:
  • $database (string) – Database name
Kembali:

TRUE on success, FALSE on failure

Return type:

bool

Select / switch the current database.

db_set_charset($charset)
Parameter:
  • $charset (string) – Character set name
Kembali:

TRUE on success, FALSE on failure

Return type:

bool

Set client character set.

platform()
Kembali:Platform name
Return type:string

The name of the platform in use (mysql, mssql, etc...).

version()
Kembali:The version of the database being used
Return type:string

Database version number.

query($sql[, $binds = FALSE[, $return_object = NULL]]])
Parameter:
  • $sql (string) – The SQL statement to execute
  • $binds (array) – An array of binding data
  • $return_object (bool) – Whether to return a result object or not
Kembali:

TRUE for successful “write-type” queries, CI_DB_result instance (method chaining) on “query” success, FALSE on failure

Return type:

mixed

Execute an SQL query.

Accepts an SQL string as input and returns a result object upon successful execution of a “read” type query.

Returns:

  • Boolean TRUE upon successful execution of a “write type” queries
  • Boolean FALSE upon failure
  • CI_DB_result object for “read type” queries
simple_query($sql)
Parameter:
  • $sql (string) – The SQL statement to execute
Kembali:

Whatever the underlying driver’s “query” function returns

Return type:

mixed

A simplified version of the query() method, appropriate for use when you don’t need to get a result object or to just send a query to the database and not care for the result.

trans_strict([$mode = TRUE])
Parameter:
  • $mode (bool) – Strict mode flag
Return type:

void

Enable/disable transaction “strict” mode.

When strict mode is enabled, if you are running multiple groups of transactions and one group fails, all groups will be rolled back.

If strict mode is disabled, each group is treated autonomously, meaning a failure of one group will not affect any others.

trans_off()
Return type:void

Disables transactions at run-time.

trans_start([$test_mode = FALSE])
Parameter:
  • $test_mode (bool) – Test mode flag
Return type:

void

Start a transaction.

trans_complete()
Return type:void

Complete Transaction.

trans_status()
Kembali:TRUE if the transaction succeeded, FALSE if it failed
Return type:bool

Lets you retrieve the transaction status flag to determine if it has failed.

compile_binds($sql, $binds)
Parameter:
  • $sql (string) – The SQL statement
  • $binds (array) – An array of binding data
Kembali:

The updated SQL statement

Return type:

string

Compiles an SQL query with the bind values passed for it.

is_write_type($sql)
Parameter:
  • $sql (string) – The SQL statement
Kembali:

TRUE if the SQL statement is of “write type”, FALSE if not

Return type:

bool

Determines if a query is of a “write” type (such as INSERT, UPDATE, DELETE) or “read” type (i.e. SELECT).

elapsed_time([$decimals = 6])
Parameter:
  • $decimals (int) – The number of decimal places
Kembali:

The aggregate query elapsed time, in microseconds

Return type:

string

Calculate the aggregate query elapsed time.

total_queries()
Kembali:The total number of queries executed
Return type:int

Returns the total number of queries that have been executed so far.

last_query()
Kembali:The last query executed
Return type:string

Returns the last query that was executed.

escape($str)
Parameter:
  • $str (mixed) – The value to escape, or an array of multiple ones
Kembali:

The escaped value(s)

Return type:

mixed

Escapes input data based on type, including boolean and NULLs.

escape_str($str[, $like = FALSE])
Parameter:
  • $str (mixed) – A string value or array of multiple ones
  • $like (bool) – Whether or not the string will be used in a LIKE condition
Kembali:

The escaped string(s)

Return type:

mixed

Escapes string values.

Peringatan

The returned strings do NOT include quotes around them.

escape_like_str($str)
Parameter:
  • $str (mixed) – A string value or array of multiple ones
Kembali:

The escaped string(s)

Return type:

mixed

Escape LIKE strings.

Similar to escape_str(), but will also escape the % and _ wildcard characters, so that they don’t cause false-positives in LIKE conditions.

primary($table)
Parameter:
  • $table (string) – Table name
Kembali:

The primary key name, FALSE if none

Return type:

string

Retrieves the primary key of a table.

Catatan

If the database platform does not support primary key detection, the first column name may be assumed as the primary key.

count_all([$table = ''])
Parameter:
  • $table (string) – Table name
Kembali:

Row count for the specified table

Return type:

int

Returns the total number of rows in a table, or 0 if no table was provided.

list_tables([$constrain_by_prefix = FALSE])
Parameter:
  • $constrain_by_prefix (bool) – TRUE to match table names by the configured dbprefix
Kembali:

Array of table names or FALSE on failure

Return type:

array

Gets a list of the tables in the current database.

table_exists($table_name)
Parameter:
  • $table_name (string) – The table name
Kembali:

TRUE if that table exists, FALSE if not

Return type:

bool

Determine if a particular table exists.

list_fields($table)
Parameter:
  • $table (string) – The table name
Kembali:

Array of field names or FALSE on failure

Return type:

array

Gets a list of the field names in a table.

field_exists($field_name, $table_name)
Parameter:
  • $table_name (string) – The table name
  • $field_name (string) – The field name
Kembali:

TRUE if that field exists in that table, FALSE if not

Return type:

bool

Determine if a particular field exists.

field_data($table)
Parameter:
  • $table (string) – The table name
Kembali:

Array of field data items or FALSE on failure

Return type:

array

Gets a list containing field data about a table.

escape_identifiers($item)
Parameter:
  • $item (mixed) – The item or array of items to escape
Kembali:

The input item(s), escaped

Return type:

mixed

Escape SQL identifiers, such as column, table and names.

insert_string($table, $data)
Parameter:
  • $table (string) – The target table
  • $data (array) – An associative array of key/value pairs
Kembali:

The SQL INSERT statement, as a string

Return type:

string

Generate an INSERT statement string.

update_string($table, $data, $where)
Parameter:
  • $table (string) – The target table
  • $data (array) – An associative array of key/value pairs
  • $where (mixed) – The WHERE statement conditions
Kembali:

The SQL UPDATE statement, as a string

Return type:

string

Generate an UPDATE statement string.

call_function($function)
Parameter:
  • $function (string) – Function name
Kembali:

The function result

Return type:

string

Runs a native PHP function , using a platform agnostic wrapper.

cache_set_path([$path = ''])
Parameter:
  • $path (string) – Path to the cache directory
Return type:

void

Sets the directory path to use for caching storage.

cache_on()
Kembali:TRUE if caching is on, FALSE if not
Return type:bool

Enable database results caching.

cache_off()
Kembali:TRUE if caching is on, FALSE if not
Return type:bool

Disable database results caching.

cache_delete([$segment_one = ''[, $segment_two = '']])
Parameter:
  • $segment_one (string) – First URI segment
  • $segment_two (string) – Second URI segment
Kembali:

TRUE on success, FALSE on failure

Return type:

bool

Delete the cache files associated with a particular URI.

cache_delete_all()
Kembali:TRUE on success, FALSE on failure
Return type:bool

Delete all cache files.

close()
Return type:void

Close the DB Connection.

display_error([$error = ''[, $swap = ''[, $native = FALSE]]])
Parameter:
  • $error (string) – The error message
  • $swap (string) – Any “swap” values
  • $native (bool) – Whether to localize the message
Return type:

void

Kembali:

Displays the DB error screensends the application/views/errors/error_db.php template

Return type:

string

Display an error message and stop script execution.

The message is displayed using the application/views/errors/error_db.php template.

protect_identifiers($item[, $prefix_single = FALSE[, $protect_identifiers = NULL[, $field_exists = TRUE]]])
Parameter:
  • $item (string) – The item to work with
  • $prefix_single (bool) – Whether to apply the dbprefix even if the input item is a single identifier
  • $protect_identifiers (bool) – Whether to quote identifiers
  • $field_exists (bool) – Whether the supplied item contains a field name or not
Kembali:

The modified item

Return type:

string

Takes a column or table name (optionally with an alias) and applies the configured dbprefix to it.

Some logic is necessary in order to deal with column names that include the path.

Consider a query like this:

SELECT * FROM hostname.database.table.column AS c FROM hostname.database.table

Or a query with aliasing:

SELECT m.member_id, m.member_name FROM members AS m

Since the column name can include up to four segments (host, DB, table, column) or also have an alias prefix, we need to do a bit of work to figure this out and insert the table prefix (if it exists) in the proper position, and escape only the correct identifiers.

This method is used extensively by the Query Builder class.