Codeigniter DB Driver Reference – This database reference has many class references. This is the platform-independent based DB implement class. This class will not be called directly. Or the adapter class for the specific database will extend and instantiate it. Here in this tutorial, we are going to explain how to use DB Driver Reference.
Codeigniter DB Driver Reference | Example
Let us understand how DB Driver Reference works in codeigniter with examples.
Class reference:-
There are following references available in DB Driver Reference. Now we will explain.
1. Initialize.
This reference is used to initialized database setting, established a connection to the database.
initialize()
- Parameters :
- Returns : TRUE on success, FALSE on failure
- Returns type : Bool
2. DB connect.
This reference is used to establish a connection with the database.
db_connect($persistent = TRUE)
- Parameters :
- $persistent (bool) : Whether to establish a persistent connection or a regular one
- Returns : Database connection resource/object or FALSE on failure
- Returns type : Mixed
3. DB pconnect.
This reference is used to establish a persistent connection with the database.
db_pconnect()
- Parameters :
- Returns : Database connection resource/object or FALSE on failure
- Returns type : Mixed
4. Reconnect.
This reference is used to keep/reestablish the database connection.
reconnect()
- Parameters :
- Returns : TRUE on success, FALSE on failure
- Returns type : Bool
5. DB select.
This reference is used to select/switch the current database.
db_select([$database = ''])
- Parameters :
- $database (string) : Database name
- Returns : TRUE on success, FALSE on failure
- Returns type : Bool
6. DB set charset.
This reference is used to set client character set.
db_set_charset($charset)
- Parameters :
- $charset (string) : Character set name
- Returns : TRUE on success, FALSE on failure
- Returns type : Bool
7. Platform.
This reference is used to display the name of platform.
platform()
- Parameters :
- Returns : Platform name.
- Returns type : String
8. Version.
This reference is used to display database number version.
version()
- Parameters :
- Returns : The version of the database being used.
- Returns type : string
9. Query.
This reference is used to execute an SQL query.
query($sql[$binds = FALSE[$return_object = NULL]])
- Parameters :
- $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
- Returns : TRUE for successful “write-type” queries, CI_DB_result instance on “query” success, FALSE on failure.
- Returns type : mixed
10. Simple query.
This reference is used to simplified version of the array method.
simple_query($sql)
- Parameters :
- $sql (string) : The SQL statement to execute
- Returns : Whatever the underlying driver’s “query” function returns.
- Returns type : mixed
11. Affected rows.
This reference is used to return the number of row changed by the last executed query.
affected_rows()
- Parameters :
- Returns : Number of rows affected.
- Returns type : int
12. Trans strict.
This reference is used to enable/disable transaction “strict” mode.
trans_strict([$mode = TRUE])
- Parameters :
- $mode (bool) : Strict mode flag.
- Returns type : void
13. Trans off.
This reference is used to disables transactions at run-time.
trans_off()
- Parameters :
- Returns type : void
14. Trans start.
This reference is used to disables transactions at run-time.
trans_start([$test_mode = FALSE])
- Parameters :
- $test_mode (bool) : Test mode flag
- Returns : TRUE on success, FALSE on failure
- Returns type : Bool
15. Trans complete.
This reference is used to complete the transaction.
trans_complete()
- Parameters :
- Returns : TRUE on success, FALSE on failure
- Returns type : Bool
16. Trans status.
This reference is used to retrieve the transaction status flag.
trans_status()
- Parameters :
- Returns : TRUE if the transaction succeeded, FALSE if it failed.
- Returns type : Bool
17. compile binds.
This reference is used to compiles an SQL query with the bind values passed for it.
compile_binds($sql, $binds)
- Parameters :
- $sql (string) : The SQL statement
- $binds (array) : An array of binding data
- Returns : The updated SQL statement.
- Returns type : String.
18. Is write type.
This reference is used to determines if a query is of a “write” type (INSERT, UPDATE, DELETE) or “read” type (SELECT).
is_write_type($sql)
- Parameters :
- $sql (string) : The SQL statement
- Returns : TRUE if the SQL statement is of “write type”, FALSE if not.
- Returns type : Bool.
19. Elapsed time.
This reference is used to calculate the aggregate query elapsed time.
elapsed_time([$decimals = 6])
- Parameters :
- $decimals (int) : The number of decimal places.
- Returns : The aggregate query elapsed time, in microseconds.
- Returns type : String.
20. Total queries.
This reference returns the total number of queries that have been executed.
total_queries()
- Parameters :
- Returns : The total number of queries executed.
- Returns type : Int.
21. Last query.
This reference returns the last query that was executed.
last_query()
- Parameters :
- Returns : The last query executed.
- Returns type : string.
22. Escape.
This reference is used to escapes input data based on type, including boolean and NULL.
escape($str)
- Parameters :
- $str (mixed) : The value to escape, or an array of multiple ones
- Returns : The escaped value.
- Returns type : mixed.
23. Escape str.
This reference is used to escapes string values.
escape_str($str[$like = FALSE])
- Parameters :
- $str (mixed) : A string value or array of multiple ones.
- $like (bool) : Whether or not the string will be used in a LIKE condition.
- Returns : The escaped string.
- Returns type : mixed.
24. Escape like str.
This reference is used to escapes like string.
escape_like_str($str)
- Parameters :
- $str (mixed) : A string value or array of multiple ones.
- Returns : The escaped string.
- Returns type : mixed.
25. Primary.
This reference is used to retrieves the primary key of a table.
primary($table)
- Parameters :
- $table (string) : Table name.
- Returns : The primary key name, FALSE if none.
- Returns type : String.
26. Count all.
This reference returns the total number of rows in a table.
count_all([$table = ''])
- Parameters :
- $table (string) : Table name.
- Returns : Row count for the specified table.
- Returns type : Int.
27. List tables.
This reference is used to get a list of the tables in the current database.
list_tables([$constrain_by_prefix = FALSE])
- Parameters :
- $constrain_by_prefix (bool) : TRUE to match table names by the configured dbprefix.
- Returns : Array of table names or FALSE on failure.
- Returns type : Array.
28. Table exists.
This reference is used to determine if a particular table exists.
table_exists($table_name)
- Parameters :
- $table_name (string) : The table name.
- Returns : TRUE if that table exists, FALSE if not.
- Returns type : Bool.
29. List fields.
This reference is used to get a list of the field names in a table.
list_fields($table)
- Parameters :
- $table (string) : The table name.
- Returns : Array of field names or FALSE on failure.
- Returns type : Array.
30. Field exists.
This reference is used to determine if a particular field exists.
field_exists($field_name, $table_name)
- Parameters :
- $table_name (string) : The table name.
- $field_name (string) : The field name.
- Returns : TRUE if that field exists in that table, FALSE if not.
- Returns type : Bool.
31. Field data.
This reference is used to get a list containing field data about a table.
field_data($table)
- Parameters :
- $table_name (string) : The table name.
- Returns : Array of field data items or FALSE on failure.
- Returns type : Bool.
32. Escape identifiers.
This reference is used to escape SQL identifiers, such as column, table and names.
escape_identifiers($item)
- Parameters :
- $item (mixed) : The item or array of items to escape.
- Returns : The input item(s), escaped.
- Returns type : Mixed.
33. Insert string.
This reference is used to generate an INSERT statement string.
insert_string($table, $data)
- Parameters :
- $table (string) : The target table.
- $data (array) : An associative array of key/value pairs.
- Returns : The SQL INSERT statement, as a string.
- Returns type : String.
34. Update string.
This reference is used to generate an UPDATE statement string.
update_string($table, $data, $where)
- Parameters :
- $table (string) : The target table.
- $data (array) : An associative array of key/value pairs.
- $where (mixed) : The WHERE statement conditions.
- Returns : The SQL UPDATE statement, as a string.
- Returns type : String.
35. Call function.
This reference is used to Runs a native PHP function , using a platform agnostic wrapper.
call_function($function)
- Parameters :
- $function (string) : Function name.
- Returns : The function result.
- Returns type : String.
36. Cache set path.
This reference is used to sets the directory path to use for caching storage.
cache_set_path([$path = ''])
- Parameters :
- $path (string) : Path to the cache directory.
- Returns type : String.
37. Cache set path.
This reference is used to sets the directory path to use for caching storage.
cache_set_path([$path = ''])
- Parameters :
- $path (string) : Path to the cache directory.
- Returns type : String.
38. Cache on.
This reference is used to disable database results caching.
cache_on()
- Parameters :
- Returns : TRUE if caching is on, FALSE if not.
- Returns type : String.
39. Cache delete.
This reference is used to delete the cache files associated with a particular URI.
cache_delete([$segment_one = ''[$segment_two = '']])
- Parameters :
- $segment_one (string) : First URI segment
- $segment_two (string) : Second URI segment
- Returns : TRUE on success, FALSE on failure.
- Returns type : Bool.
40. Cache delete all.
This reference is used to delete all cache files.
cache_delete_all()
- Parameters :
- Returns : TRUE on success, FALSE on failure.
- Returns type : Bool.
41. Close.
This reference is used to close the DB Connection.
close()
- Parameters :
- Returns type : void.
42. Display error.
This reference is used to display an error message and stop script execution.
display_error([$error = ''[$swap = ''[native = FALSE]]])
- Parameters :
- $error (string) : The error message
- $swap (string) : Any “swap” values
- $native (bool) : Whether to localize the message
- Returns type : void.
43. Protect identifiers.
This reference is used to takes a column or table name and applies the configured dbprefix to it.
protect_identifiers($item[$prefix_single = FALSE[$protect_identifiers = NULL[$field_exists = TRUE]]])
- Parameters :
- $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
- Returns : The modified item
- Returns type : string.