class CI_DB_query_builder
-
reset_query()
-
Returns: CI_DB_query_builder instance (method chaining) Return type: CI_DB_query_builder Resets the current Query Builder state. Useful when you want to build a query that can be cancelled under certain conditions.
-
start_cache()
-
Returns: CI_DB_query_builder instance (method chaining) Return type: CI_DB_query_builder Starts the Query Builder cache.
-
stop_cache()
-
Returns: CI_DB_query_builder instance (method chaining) Return type: CI_DB_query_builder Stops the Query Builder cache.
-
flush_cache()
-
Returns: CI_DB_query_builder instance (method chaining) Return type: CI_DB_query_builder Empties the Query Builder cache.
-
set_dbprefix([$prefix = ''])
-
Parameters: - $prefix (string) – The new prefix to use
Returns: The DB prefix in use
Return type: string
Sets the database prefix, without having to reconnect.
-
dbprefix([$table = ''])
-
Parameters: - $table (string) – The table name to prefix
Returns: The prefixed table name
Return type: string
Prepends a database prefix, if one exists in configuration.
-
count_all_results([$table = ''[, $reset = TRUE]])
-
Parameters: - $table (string) – Table name
- $reset (bool) – Whether to reset values for SELECTs
Returns: Number of rows in the query result
Return type: int
Generates a platform-specific query string that counts all records returned by an Query Builder query.
-
get([$table = ''[, $limit = NULL[, $offset = NULL]]])
-
Parameters: - $table (string) – The table to query
- $limit (int) – The LIMIT clause
- $offset (int) – The OFFSET clause
Returns: CI_DB_result instance (method chaining)
Return type: CI_DB_result
Compiles and runs SELECT statement based on the already called Query Builder methods.
-
get_where([$table = ''[, $where = NULL[, $limit = NULL[, $offset = NULL]]]])
-
Parameters: - $table (mixed) – The table(s) to fetch data from; string or array
- $where (string) – The WHERE clause
- $limit (int) – The LIMIT clause
- $offset (int) – The OFFSET clause
Returns: CI_DB_result instance (method chaining)
Return type: CI_DB_result
Same as
get()
, but also allows the WHERE to be added directly.
-
select([$select = '*'[, $escape = NULL]])
-
Parameters: - $select (string) – The SELECT portion of a query
- $escape (bool) – Whether to escape values and identifiers
Returns: CI_DB_query_builder instance (method chaining)
Return type: CI_DB_query_builder
Adds a SELECT clause to a query.
-
select_avg([$select = ''[, $alias = '']])
-
Parameters: - $select (string) – Field to compute the average of
- $alias (string) – Alias for the resulting value name
Returns: CI_DB_query_builder instance (method chaining)
Return type: CI_DB_query_builder
Adds a SELECT AVG(field) clause to a query.
-
select_max([$select = ''[, $alias = '']])
-
Parameters: - $select (string) – Field to compute the maximum of
- $alias (string) – Alias for the resulting value name
Returns: CI_DB_query_builder instance (method chaining)
Return type: CI_DB_query_builder
Adds a SELECT MAX(field) clause to a query.
-
select_min([$select = ''[, $alias = '']])
-
Parameters: - $select (string) – Field to compute the minimum of
- $alias (string) – Alias for the resulting value name
Returns: CI_DB_query_builder instance (method chaining)
Return type: CI_DB_query_builder
Adds a SELECT MIN(field) clause to a query.
-
select_sum([$select = ''[, $alias = '']])
-
Parameters: - $select (string) – Field to compute the sum of
- $alias (string) – Alias for the resulting value name
Returns: CI_DB_query_builder instance (method chaining)
Return type: CI_DB_query_builder
Adds a SELECT SUM(field) clause to a query.
-
distinct([$val = TRUE])
-
Parameters: - $val (bool) – Desired value of the “distinct” flag
Returns: CI_DB_query_builder instance (method chaining)
Return type: CI_DB_query_builder
Sets a flag which tells the query builder to add a DISTINCT clause to the SELECT portion of the query.
-
from($from)
-
Parameters: - $from (mixed) – Table name(s); string or array
Returns: CI_DB_query_builder instance (method chaining)
Return type: CI_DB_query_builder
Specifies the FROM clause of a query.
-
join($table, $cond[, $type = ''[, $escape = NULL]])
-
Parameters: - $table (string) – Table name to join
- $cond (string) – The JOIN ON condition
- $type (string) – The JOIN type
- $escape (bool) – Whether to escape values and identifiers
Returns: CI_DB_query_builder instance (method chaining)
Return type: CI_DB_query_builder
Adds a JOIN clause to a query.
-
where($key[, $value = NULL[, $escape = NULL]])
-
Parameters: - $key (mixed) – Name of field to compare, or associative array
- $value (mixed) – If a single key, compared to this value
- $escape (bool) – Whether to escape values and identifiers
Returns: DB_query_builder instance
Return type: object
Generates the WHERE portion of the query. Separates multiple calls with ‘AND’.
-
or_where($key[, $value = NULL[, $escape = NULL]])
-
Parameters: - $key (mixed) – Name of field to compare, or associative array
- $value (mixed) – If a single key, compared to this value
- $escape (bool) – Whether to escape values and identifiers
Returns: DB_query_builder instance
Return type: object
Generates the WHERE portion of the query. Separates multiple calls with ‘OR’.
-
or_where_in([$key = NULL[, $values = NULL[, $escape = NULL]]])
-
Parameters: - $key (string) – The field to search
- $values (array) – The values searched on
- $escape (bool) – Whether to escape values and identifiers
Returns: DB_query_builder instance
Return type: object
Generates a WHERE field IN(‘item’, ‘item’) SQL query, joined with ‘OR’ if appropriate.
-
or_where_not_in([$key = NULL[, $values = NULL[, $escape = NULL]]])
-
Parameters: - $key (string) – The field to search
- $values (array) – The values searched on
- $escape (bool) – Whether to escape values and identifiers
Returns: DB_query_builder instance
Return type: object
Generates a WHERE field NOT IN(‘item’, ‘item’) SQL query, joined with ‘OR’ if appropriate.
-
where_in([$key = NULL[, $values = NULL[, $escape = NULL]]])
-
Parameters: - $key (string) – Name of field to examine
- $values (array) – Array of target values
- $escape (bool) – Whether to escape values and identifiers
Returns: DB_query_builder instance
Return type: object
Generates a WHERE field IN(‘item’, ‘item’) SQL query, joined with ‘AND’ if appropriate.
-
where_not_in([$key = NULL[, $values = NULL[, $escape = NULL]]])
-
Parameters: - $key (string) – Name of field to examine
- $values (array) – Array of target values
- $escape (bool) – Whether to escape values and identifiers
Returns: DB_query_builder instance
Return type: object
Generates a WHERE field NOT IN(‘item’, ‘item’) SQL query, joined with ‘AND’ if appropriate.
-
group_start()
-
Returns: CI_DB_query_builder instance (method chaining) Return type: CI_DB_query_builder Starts a group expression, using ANDs for the conditions inside it.
-
or_group_start()
-
Returns: CI_DB_query_builder instance (method chaining) Return type: CI_DB_query_builder Starts a group expression, using ORs for the conditions inside it.
-
not_group_start()
-
Returns: CI_DB_query_builder instance (method chaining) Return type: CI_DB_query_builder Starts a group expression, using AND NOTs for the conditions inside it.
-
or_not_group_start()
-
Returns: CI_DB_query_builder instance (method chaining) Return type: CI_DB_query_builder Starts a group expression, using OR NOTs for the conditions inside it.
-
group_end()
-
Returns: DB_query_builder instance Return type: object Ends a group expression.
-
like($field[, $match = ''[, $side = 'both'[, $escape = NULL]]])
-
Parameters: - $field (string) – Field name
- $match (string) – Text portion to match
- $side (string) – Which side of the expression to put the ‘%’ wildcard on
- $escape (bool) – Whether to escape values and identifiers
Returns: CI_DB_query_builder instance (method chaining)
Return type: CI_DB_query_builder
Adds a LIKE clause to a query, separating multiple calls with AND.
-
or_like($field[, $match = ''[, $side = 'both'[, $escape = NULL]]])
-
Parameters: - $field (string) – Field name
- $match (string) – Text portion to match
- $side (string) – Which side of the expression to put the ‘%’ wildcard on
- $escape (bool) – Whether to escape values and identifiers
Returns: CI_DB_query_builder instance (method chaining)
Return type: CI_DB_query_builder
Adds a LIKE clause to a query, separating multiple class with OR.
-
not_like($field[, $match = ''[, $side = 'both'[, $escape = NULL]]])
-
Parameters: - $field (string) – Field name
- $match (string) – Text portion to match
- $side (string) – Which side of the expression to put the ‘%’ wildcard on
- $escape (bool) – Whether to escape values and identifiers
Returns: CI_DB_query_builder instance (method chaining)
Return type: CI_DB_query_builder
Adds a NOT LIKE clause to a query, separating multiple calls with AND.
-
or_not_like($field[, $match = ''[, $side = 'both'[, $escape = NULL]]])
-
Parameters: - $field (string) – Field name
- $match (string) – Text portion to match
- $side (string) – Which side of the expression to put the ‘%’ wildcard on
- $escape (bool) – Whether to escape values and identifiers
Returns: CI_DB_query_builder instance (method chaining)
Return type: CI_DB_query_builder
Adds a NOT LIKE clause to a query, separating multiple calls with OR.
-
having($key[, $value = NULL[, $escape = NULL]])
-
Parameters: - $key (mixed) – Identifier (string) or associative array of field/value pairs
- $value (string) – Value sought if $key is an identifier
- $escape (string) – Whether to escape values and identifiers
Returns: CI_DB_query_builder instance (method chaining)
Return type: CI_DB_query_builder
Adds a HAVING clause to a query, separating multiple calls with AND.
-
or_having($key[, $value = NULL[, $escape = NULL]])
-
Parameters: - $key (mixed) – Identifier (string) or associative array of field/value pairs
- $value (string) – Value sought if $key is an identifier
- $escape (string) – Whether to escape values and identifiers
Returns: CI_DB_query_builder instance (method chaining)
Return type: CI_DB_query_builder
Adds a HAVING clause to a query, separating multiple calls with OR.
-
group_by($by[, $escape = NULL])
-
Parameters: - $by (mixed) – Field(s) to group by; string or array
Returns: CI_DB_query_builder instance (method chaining)
Return type: CI_DB_query_builder
Adds a GROUP BY clause to a query.
-
order_by($orderby[, $direction = ''[, $escape = NULL]])
-
Parameters: - $orderby (string) – Field to order by
- $direction (string) – The order requested - ASC, DESC or random
- $escape (bool) – Whether to escape values and identifiers
Returns: CI_DB_query_builder instance (method chaining)
Return type: CI_DB_query_builder
Adds an ORDER BY clause to a query.
-
limit($value[, $offset = 0])
-
Parameters: - $value (int) – Number of rows to limit the results to
- $offset (int) – Number of rows to skip
Returns: CI_DB_query_builder instance (method chaining)
Return type: CI_DB_query_builder
Adds LIMIT and OFFSET clauses to a query.
-
offset($offset)
-
Parameters: - $offset (int) – Number of rows to skip
Returns: CI_DB_query_builder instance (method chaining)
Return type: CI_DB_query_builder
Adds an OFFSET clause to a query.
-
set($key[, $value = ''[, $escape = NULL]])
-
Parameters: - $key (mixed) – Field name, or an array of field/value pairs
- $value (string) – Field value, if $key is a single field
- $escape (bool) – Whether to escape values and identifiers
Returns: CI_DB_query_builder instance (method chaining)
Return type: CI_DB_query_builder
Adds field/value pairs to be passed later to
insert()
,update()
orreplace()
.
-
insert([$table = ''[, $set = NULL[, $escape = NULL]]])
-
Parameters: - $table (string) – Table name
- $set (array) – An associative array of field/value pairs
- $escape (bool) – Whether to escape values and identifiers
Returns: TRUE on success, FALSE on failure
Return type: bool
Compiles and executes an INSERT statement.
-
insert_batch($table[, $set = NULL[, $escape = NULL[, $batch_size = 100]]])
-
Parameters: - $table (string) – Table name
- $set (array) – Data to insert
- $escape (bool) – Whether to escape values and identifiers
- $batch_size (int) – Count of rows to insert at once
Returns: Number of rows inserted or FALSE on failure
Return type: mixed
Compiles and executes batch
INSERT
statements.Note
When more than
$batch_size
rows are provided, multipleINSERT
queries will be executed, each trying to insert up to$batch_size
rows.
-
set_insert_batch($key[, $value = ''[, $escape = NULL]])
-
Parameters: - $key (mixed) – Field name or an array of field/value pairs
- $value (string) – Field value, if $key is a single field
- $escape (bool) – Whether to escape values and identifiers
Returns: CI_DB_query_builder instance (method chaining)
Return type: CI_DB_query_builder
Adds field/value pairs to be inserted in a table later via
insert_batch()
.
-
update([$table = ''[, $set = NULL[, $where = NULL[, $limit = NULL]]]])
-
Parameters: - $table (string) – Table name
- $set (array) – An associative array of field/value pairs
- $where (string) – The WHERE clause
- $limit (int) – The LIMIT clause
Returns: TRUE on success, FALSE on failure
Return type: bool
Compiles and executes an UPDATE statement.
-
update_batch($table[, $set = NULL[, $value = NULL[, $batch_size = 100]]])
-
Parameters: - $table (string) – Table name
- $set (array) – Field name, or an associative array of field/value pairs
- $value (string) – Field value, if $set is a single field
- $batch_size (int) – Count of conditions to group in a single query
Returns: Number of rows updated or FALSE on failure
Return type: mixed
Compiles and executes batch
UPDATE
statements.Note
When more than
$batch_size
field/value pairs are provided, multiple queries will be executed, each handling up to$batch_size
field/value pairs.
-
set_update_batch($key[, $value = ''[, $escape = NULL]])
-
Parameters: - $key (mixed) – Field name or an array of field/value pairs
- $value (string) – Field value, if $key is a single field
- $escape (bool) – Whether to escape values and identifiers
Returns: CI_DB_query_builder instance (method chaining)
Return type: CI_DB_query_builder
Adds field/value pairs to be updated in a table later via
update_batch()
.
-
replace([$table = ''[, $set = NULL]])
-
Parameters: - $table (string) – Table name
- $set (array) – An associative array of field/value pairs
Returns: TRUE on success, FALSE on failure
Return type: bool
Compiles and executes a REPLACE statement.
-
delete([$table = ''[, $where = ''[, $limit = NULL[, $reset_data = TRUE]]]])
-
Parameters: - $table (mixed) – The table(s) to delete from; string or array
- $where (string) – The WHERE clause
- $limit (int) – The LIMIT clause
- $reset_data (bool) – TRUE to reset the query “write” clause
Returns: CI_DB_query_builder instance (method chaining) or FALSE on failure
Return type: mixed
Compiles and executes a DELETE query.
-
truncate([$table = ''])
-
Parameters: - $table (string) – Table name
Returns: TRUE on success, FALSE on failure
Return type: bool
Executes a TRUNCATE statement on a table.
Note
If the database platform in use doesn’t support TRUNCATE, a DELETE statement will be used instead.
-
empty_table([$table = ''])
-
Parameters: - $table (string) – Table name
Returns: TRUE on success, FALSE on failure
Return type: bool
Deletes all records from a table via a DELETE statement.
-
get_compiled_select([$table = ''[, $reset = TRUE]])
-
Parameters: - $table (string) – Table name
- $reset (bool) – Whether to reset the current QB values or not
Returns: The compiled SQL statement as a string
Return type: string
Compiles a SELECT statement and returns it as a string.
-
get_compiled_insert([$table = ''[, $reset = TRUE]])
-
Parameters: - $table (string) – Table name
- $reset (bool) – Whether to reset the current QB values or not
Returns: The compiled SQL statement as a string
Return type: string
Compiles an INSERT statement and returns it as a string.
-
get_compiled_update([$table = ''[, $reset = TRUE]])
-
Parameters: - $table (string) – Table name
- $reset (bool) – Whether to reset the current QB values or not
Returns: The compiled SQL statement as a string
Return type: string
Compiles an UPDATE statement and returns it as a string.
-
get_compiled_delete([$table = ''[, $reset = TRUE]])
-
Parameters: - $table (string) – Table name
- $reset (bool) – Whether to reset the current QB values or not
Returns: The compiled SQL statement as a string
Return type: string
Compiles a DELETE statement and returns it as a string.
Please login to continue.