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.
Important
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()
Returns: | TRUE on success, FALSE on failure |
---|---|
Return type: | bool |
Initialize database settings, establish a connection to the database.
db_connect($persistent = TRUE)
Parameters: |
|
---|---|
Returns: |
Database connection resource/object or FALSE on failure |
Return type: |
mixed |
Establish a connection with the database.
Note
The returned value depends on the underlying driver in use. For example, a mysqli
instance will be returned with the ‘mysqli’ driver.
db_pconnect()
Returns: | Database connection resource/object or FALSE on failure |
---|---|
Return type: | mixed |
Establish a persistent connection with the database.
Note
This method is just an alias for db_connect(TRUE)
.
reconnect()
Returns: | 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 = ''])
Parameters: |
|
---|---|
Returns: |
TRUE on success, FALSE on failure |
Return type: |
bool |
Select / switch the current database.
db_set_charset($charset)
Parameters: |
|
---|---|
Returns: |
TRUE on success, FALSE on failure |
Return type: |
bool |
Set client character set.
platform()
Returns: | Platform name |
---|---|
Return type: | string |
The name of the platform in use (mysql, mssql, etc…).
version()
Returns: | The version of the database being used |
---|---|
Return type: | string |
Database version number.
query($sql[, $binds = FALSE[, $return_object = NULL]])
Parameters: |
|
---|---|
Returns: |
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:
CI_DB_result
object for “read type” queriessimple_query($sql)
Parameters: |
|
---|---|
Returns: |
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.
affected_rows()
Returns: | Number of rows affected |
---|---|
Return type: | int |
Returns the number of rows changed by the last executed query.
Useful for checking how much rows were created, updated or deleted during the last executed query.
trans_strict([$mode = TRUE])
Parameters: |
|
---|---|
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 subsequent 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])
Parameters: |
|
---|---|
Returns: |
TRUE on success, FALSE on failure |
Return type: |
bool |
Start a transaction.
trans_complete()
Returns: | TRUE on success, FALSE on failure |
---|---|
Return type: | bool |
Complete Transaction.
trans_status()
Returns: | 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)
Parameters: |
|
---|---|
Returns: |
The updated SQL statement |
Return type: |
string |
Compiles an SQL query with the bind values passed for it.
is_write_type($sql)
Parameters: |
|
---|---|
Returns: |
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])
Parameters: |
|
---|---|
Returns: |
The aggregate query elapsed time, in microseconds |
Return type: |
string |
Calculate the aggregate query elapsed time.
total_queries()
Returns: | The total number of queries executed |
---|---|
Return type: | int |
Returns the total number of queries that have been executed so far.
last_query()
Returns: | The last query executed |
---|---|
Return type: | string |
Returns the last query that was executed.
escape($str)
Parameters: |
|
---|---|
Returns: |
The escaped value(s) |
Return type: |
mixed |
Escapes input data based on type, including boolean and NULLs.
escape_str($str[, $like = FALSE])
Parameters: |
|
---|---|
Returns: |
The escaped string(s) |
Return type: |
mixed |
Escapes string values.
Warning
The returned strings do NOT include quotes around them.
escape_like_str($str)
Parameters: |
|
---|---|
Returns: |
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.
Important
The escape_like_str()
method uses ‘!’ (exclamation mark) to escape special characters for LIKE conditions. Because this method escapes partial strings that you would wrap in quotes yourself, it cannot automatically add the ESCAPE '!'
condition for you, and so you’ll have to manually do that.
primary($table)
Parameters: |
|
---|---|
Returns: |
The primary key name, FALSE if none |
Return type: |
string |
Retrieves the primary key of a table.
Note
If the database platform does not support primary key detection, the first column name may be assumed as the primary key.
count_all([$table = ''])
Parameters: |
|
---|---|
Returns: |
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])
Parameters: |
|
---|---|
Returns: |
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)
Parameters: |
|
---|---|
Returns: |
TRUE if that table exists, FALSE if not |
Return type: |
bool |
Determine if a particular table exists.
list_fields($table)
Parameters: |
|
---|---|
Returns: |
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)
Parameters: |
|
---|---|
Returns: |
TRUE if that field exists in that table, FALSE if not |
Return type: |
bool |
Determine if a particular field exists.
field_data($table)
Parameters: |
|
---|---|
Returns: |
Array of field data items or FALSE on failure |
Return type: |
array |
Gets a list containing field data about a table.
escape_identifiers($item)
Parameters: |
|
---|---|
Returns: |
The input item(s), escaped |
Return type: |
mixed |
Escape SQL identifiers, such as column, table and names.
insert_string($table, $data)
Parameters: |
|
---|---|
Returns: |
The SQL INSERT statement, as a string |
Return type: |
string |
Generate an INSERT statement string.
update_string($table, $data, $where)
Parameters: |
|
---|---|
Returns: |
The SQL UPDATE statement, as a string |
Return type: |
string |
Generate an UPDATE statement string.
call_function($function)
Parameters: |
|
---|---|
Returns: |
The function result |
Return type: |
string |
Runs a native PHP function , using a platform agnostic wrapper.
cache_set_path([$path = ''])
Parameters: |
|
---|---|
Return type: |
void |
Sets the directory path to use for caching storage.
cache_on()
Returns: | TRUE if caching is on, FALSE if not |
---|---|
Return type: | bool |
Enable database results caching.
cache_off()
Returns: | TRUE if caching is on, FALSE if not |
---|---|
Return type: | bool |
Disable database results caching.
cache_delete([$segment_one = ''[, $segment_two = '']])
Parameters: |
|
---|---|
Returns: |
TRUE on success, FALSE on failure |
Return type: |
bool |
Delete the cache files associated with a particular URI.
cache_delete_all()
Returns: | 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]]])
Parameters: |
|
---|---|
Return type: |
void |
Returns: |
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]]])
Parameters: |
|
---|---|
Returns: |
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.
© 2014–2018 British Columbia Institute of Technology
Licensed under the MIT License.
https://www.codeigniter.com/user_guide/database/db_driver_reference.html