diff options
Diffstat (limited to 'user_guide_src/source/database/query_builder.rst')
-rw-r--r-- | user_guide_src/source/database/query_builder.rst | 57 |
1 files changed, 37 insertions, 20 deletions
diff --git a/user_guide_src/source/database/query_builder.rst b/user_guide_src/source/database/query_builder.rst index fa1e90353..5d9ae4592 100644 --- a/user_guide_src/source/database/query_builder.rst +++ b/user_guide_src/source/database/query_builder.rst @@ -520,13 +520,19 @@ The second parameter lets you set a result offset. Permits you to determine the number of rows in a particular Active Record query. Queries will accept Query Builder restrictors such as -where(), or_where(), like(), or_like(), etc. Example:: +``where()``, ``or_where()``, ``like()``, ``or_like()``, etc. Example:: echo $this->db->count_all_results('my_table'); // Produces an integer, like 25 $this->db->like('title', 'match'); $this->db->from('my_table'); echo $this->db->count_all_results(); // Produces an integer, like 17 +However, this method also resets any field values that you may have passed +to ``select()``. If you need to keep them, you can pass ``FALSE`` as the +second parameter:: + + echo $this->db->count_all_results('my_table', FALSE); + **$this->db->count_all()** Permits you to determine the number of rows in a particular table. @@ -634,18 +640,18 @@ Example:: $sql = $this->db->set($data)->get_compiled_insert('mytable'); echo $sql; - // Produces string: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date') + // Produces string: INSERT INTO mytable (`title`, `name`, `date`) VALUES ('My title', 'My name', 'My date') The second parameter enables you to set whether or not the query builder query will be reset (by default it will be--just like $this->db->insert()):: echo $this->db->set('title', 'My Title')->get_compiled_insert('mytable', FALSE); - // Produces string: INSERT INTO mytable (title) VALUES ('My Title') + // Produces string: INSERT INTO mytable (`title`) VALUES ('My Title') echo $this->db->set('content', 'My Content')->get_compiled_insert(); - // Produces string: INSERT INTO mytable (title, content) VALUES ('My Title', 'My Content') + // Produces string: INSERT INTO mytable (`title`, `content`) VALUES ('My Title', 'My Content') The key thing to notice in the above example is that the second query did not utlize `$this->db->from()` nor did it pass a table name into the first @@ -724,7 +730,7 @@ or update functions:** :: $this->db->set('name', $name); - $this->db->insert('mytable'); // Produces: INSERT INTO mytable (name) VALUES ('{$name}') + $this->db->insert('mytable'); // Produces: INSERT INTO mytable (`name`) VALUES ('{$name}') If you use multiple function called they will be assembled properly based on whether you are doing an insert or an update:: @@ -734,18 +740,20 @@ based on whether you are doing an insert or an update:: $this->db->set('status', $status); $this->db->insert('mytable'); -**set()** will also accept an optional third parameter ($escape), that +**set()** will also accept an optional third parameter (``$escape``), that will prevent data from being escaped if set to FALSE. To illustrate the -difference, here is set() used both with and without the escape +difference, here is ``set()`` used both with and without the escape parameter. :: $this->db->set('field', 'field+1', FALSE); - $this->db->insert('mytable'); // gives INSERT INTO mytable (field) VALUES (field+1) - $this->db->set('field', 'field+1'); - $this->db->insert('mytable'); // gives INSERT INTO mytable (field) VALUES ('field+1') + $this->db->where('id', 2); + $this->db->update('mytable'); // gives UPDATE mytable SET field = field+1 WHERE id = 2 + $this->db->set('field', 'field+1'); + $this->db->where('id', 2); + $this->db->update('mytable'); // gives UPDATE `mytable` SET `field` = 'field+1' WHERE `id` = 2 You can also pass an associative array to this function:: @@ -786,7 +794,11 @@ is an example using an array:: $this->db->where('id', $id); $this->db->update('mytable', $data); - // Produces: // UPDATE mytable // SET title = '{$title}', name = '{$name}', date = '{$date}' // WHERE id = $id + // Produces: + // + // UPDATE mytable + // SET title = '{$title}', name = '{$name}', date = '{$date}' + // WHERE id = $id Or you can supply an object:: @@ -801,7 +813,11 @@ Or you can supply an object:: $object = new Myclass; $this->db->where('id', $id); $this->db->update('mytable', $object); - // Produces: // UPDATE mytable // SET title = '{$title}', name = '{$name}', date = '{$date}' // WHERE id = $id + // Produces: + // + // UPDATE `mytable` + // SET `title` = '{$title}', `name` = '{$name}', `date` = '{$date}' + // WHERE id = `$id` .. note:: All values are escaped automatically producing safer queries. @@ -1002,7 +1018,7 @@ Here's a usage example:: .. note:: The following statements can be cached: select, from, join, - where, like, group_by, having, order_by, set + where, like, group_by, having, order_by *********************** @@ -1091,9 +1107,10 @@ Class Reference Prepends a database prefix, if one exists in configuration. - .. php:method:: count_all_results([$table = '']) + .. php:method:: count_all_results([$table = '', [$reset = TRUE]]) :param string $table: Table name + :param bool $reset: Whether to reset values for SELECTs :returns: Number of rows in the query result :rtype: int @@ -1199,7 +1216,7 @@ Class Reference :param mixed $key: Name of field to compare, or associative array :param mixed $value: If a single key, compared to this value - :param boolean $escape: Whether to escape values and identifiers + :param bool $escape: Whether to escape values and identifiers :returns: DB_query_builder instance :rtype: object @@ -1210,7 +1227,7 @@ Class Reference :param mixed $key: Name of field to compare, or associative array :param mixed $value: If a single key, compared to this value - :param boolean $escape: Whether to escape values and identifiers + :param bool $escape: Whether to escape values and identifiers :returns: DB_query_builder instance :rtype: object @@ -1221,7 +1238,7 @@ Class Reference :param string $key: The field to search :param array $values: The values searched on - :param boolean $escape: Whether to escape values and identifiers + :param bool $escape: Whether to escape values and identifiers :returns: DB_query_builder instance :rtype: object @@ -1232,7 +1249,7 @@ Class Reference :param string $key: The field to search :param array $values: The values searched on - :param boolean $escape: Whether to escape values and identifiers + :param bool $escape: Whether to escape values and identifiers :returns: DB_query_builder instance :rtype: object @@ -1243,7 +1260,7 @@ Class Reference :param string $key: Name of field to examine :param array $values: Array of target values - :param boolean $escape: Whether to escape values and identifiers + :param bool $escape: Whether to escape values and identifiers :returns: DB_query_builder instance :rtype: object @@ -1254,7 +1271,7 @@ Class Reference :param string $key: Name of field to examine :param array $values: Array of target values - :param boolean $escape: Whether to escape values and identifiers + :param bool $escape: Whether to escape values and identifiers :returns: DB_query_builder instance :rtype: object |