diff options
author | Andrey Andreev <narf@devilix.net> | 2014-11-25 10:17:30 +0100 |
---|---|---|
committer | Andrey Andreev <narf@devilix.net> | 2014-11-25 10:17:30 +0100 |
commit | 809a1b516d4760fe631f8d3ffa767b9f9310dacc (patch) | |
tree | 473d9c9ff073b351de87fa49b3369200289455ab | |
parent | 8371eb1f77b0a325cb89ad437d572ef1b94f5cf4 (diff) | |
parent | 8b85526c6a9758ef92340ba0df6a32249a578b39 (diff) |
Merge pull request #3369 from jim-parry/userguide/database
Userguide/database - part 1
-rw-r--r-- | user_guide_src/source/database/helpers.rst | 61 | ||||
-rw-r--r-- | user_guide_src/source/database/index.rst | 2 | ||||
-rw-r--r-- | user_guide_src/source/database/queries.rst | 24 | ||||
-rw-r--r-- | user_guide_src/source/database/results.rst | 43 |
4 files changed, 71 insertions, 59 deletions
diff --git a/user_guide_src/source/database/helpers.rst b/user_guide_src/source/database/helpers.rst index 77bf1b5d2..2d997a9e0 100644 --- a/user_guide_src/source/database/helpers.rst +++ b/user_guide_src/source/database/helpers.rst @@ -1,9 +1,11 @@ -###################### -Query Helper Functions -###################### +#################### +Query Helper Methods +#################### -$this->db->insert_id() -====================== +Information From Executing a Query +================================== + +**$this->db->insert_id()** The insert ID number when performing database inserts. @@ -11,8 +13,7 @@ The insert ID number when performing database inserts. driver, this function requires a $name parameter, which specifies the appropriate sequence to check for the insert id. -$this->db->affected_rows() -========================== +**$this->db->affected_rows()** Displays the number of affected rows, when doing "write" type queries (insert, update, etc.). @@ -22,8 +23,23 @@ Displays the number of affected rows, when doing "write" type queries affected rows. By default this hack is enabled but it can be turned off in the database driver file. -$this->db->count_all() -====================== +**$this->db->last_query()** + +Returns the last query that was run (the query string, not the result). +Example:: + + $str = $this->db->last_query(); + + // Produces: SELECT * FROM sometable.... + + +.. note:: Disabling the **save_queries** setting in your database + configuration will render this function useless. + +Information About Your Database +=============================== + +**$this->db->count_all()** Permits you to determine the number of rows in a particular table. Submit the table name in the first parameter. Example:: @@ -32,38 +48,24 @@ Submit the table name in the first parameter. Example:: // Produces an integer, like 25 -$this->db->platform() -===================== +**$this->db->platform()** Outputs the database platform you are running (MySQL, MS SQL, Postgres, etc...):: echo $this->db->platform(); -$this->db->version() -==================== +**$this->db->version()** Outputs the database version you are running:: echo $this->db->version(); -$this->db->last_query() -======================= - -Returns the last query that was run (the query string, not the result). -Example:: - - $str = $this->db->last_query(); - - // Produces: SELECT * FROM sometable.... - - -.. note:: Disabling the **save_queries** setting in your database - configuration will render this function useless. - -$this->db->insert_string() +Making Your Queries Easier ========================== +**$this->db->insert_string()** + This function simplifies the process of writing database inserts. It returns a correctly formatted SQL insert string. Example:: @@ -78,8 +80,7 @@ array with the data to be inserted. The above example produces:: .. note:: Values are automatically escaped, producing safer queries. -$this->db->update_string() -========================== +**$this->db->update_string()** This function simplifies the process of writing database updates. It returns a correctly formatted SQL update string. Example:: diff --git a/user_guide_src/source/database/index.rst b/user_guide_src/source/database/index.rst index 7ccb8fb00..cfd624238 100644 --- a/user_guide_src/source/database/index.rst +++ b/user_guide_src/source/database/index.rst @@ -1,5 +1,5 @@ ################## -The Database Class +Database Reference ################## CodeIgniter comes with a full-featured and very fast abstracted database diff --git a/user_guide_src/source/database/queries.rst b/user_guide_src/source/database/queries.rst index 76ff1083f..43a0a30bf 100644 --- a/user_guide_src/source/database/queries.rst +++ b/user_guide_src/source/database/queries.rst @@ -2,10 +2,14 @@ Queries ####### -$this->db->query(); -=================== +************ +Query Basics +************ -To submit a query, use the following function:: +Regular Queries +=============== + +To submit a query, use the **query** function:: $this->db->query('YOUR QUERY HERE'); @@ -18,10 +22,11 @@ this:: $query = $this->db->query('YOUR QUERY HERE'); -$this->db->simple_query(); -========================== +Simplified Queries +================== -This is a simplified version of the $this->db->query() method. It DOES +The **simple_query** method is a simplified version of the +$this->db->query() method. It DOES NOT return a database result set, nor does it set the query timer, or compile bind data, or store your query for debugging. It simply lets you submit a query. Most users will rarely use this function. @@ -116,7 +121,9 @@ this: :: - $search = '20% raise'; $sql = "SELECT id FROM table WHERE column LIKE '%".$this->db->escape_like_str($search)."%'"; + $search = '20% raise'; + $sql = "SELECT id FROM table WHERE column LIKE '%" . + $this->db->escape_like_str($search)."%'"; ************** @@ -150,8 +157,7 @@ you. Handling Errors *************** -$this->db->error(); -=================== +**$this->db->error();** If you need to get the last error that has occured, the error() method will return an array containing its code and message. Here's a quick diff --git a/user_guide_src/source/database/results.rst b/user_guide_src/source/database/results.rst index e0a87a851..e06985130 100644 --- a/user_guide_src/source/database/results.rst +++ b/user_guide_src/source/database/results.rst @@ -4,10 +4,14 @@ Generating Query Results There are several ways to generate query results: +************* +Result Arrays +************* + result() ======== -This function returns the query result as an array of **objects**, or +This method returns the query result as an array of **objects**, or **an empty array** on failure. Typically you'll use this in a foreach loop, like this:: @@ -20,7 +24,7 @@ loop, like this:: echo $row->body; } -The above function is an alias of result_object(). +The above method is an alias of result_object(). If you run queries that might **not** produce a result, you are encouraged to test the result first:: @@ -53,7 +57,7 @@ instantiate for each result object (note: this class must be loaded) result_array() =============== -This function returns the query result as a pure array, or an empty +This method returns the query result as a pure array, or an empty array when no result is produced. Typically you'll use this in a foreach loop, like this:: @@ -66,10 +70,14 @@ loop, like this:: echo $row['body']; } +*********** +Result Rows +*********** + row() ===== -This function returns a single result row. If your query has more than +This method returns a single result row. If your query has more than one row, it returns only the first row. The result is returned as an **object**. Here's a usage example:: @@ -101,7 +109,7 @@ to instantiate the row with:: row_array() =========== -Identical to the above row() function, except it returns an array. +Identical to the above row() method, except it returns an array. Example:: $query = $this->db->query("YOUR QUERY"); @@ -136,7 +144,8 @@ parameter: | **$row = $query->next_row('array')** | **$row = $query->previous_row('array')** -.. note:: all the functions above will load the whole result into memory (prefetching) use unbuffered_row() for processing large result sets. +.. note:: all the methods above will load the whole result into memory + (prefetching) use unbuffered_row() for processing large result sets. unbuffered_row() ================ @@ -163,12 +172,11 @@ the returned value's type:: $query->unbuffered_row('object'); // object $query->unbuffered_row('array'); // associative array -*********************** -Result Helper Functions -*********************** +********************* +Result Helper Methods +********************* -$query->num_rows() -================== +**$query->num_rows()** The number of rows returned by the query. Note: In this example, $query is the variable that the query result object is assigned to:: @@ -181,20 +189,18 @@ is the variable that the query result object is assigned to:: Not all database drivers have a native way of getting the total number of rows for a result set. When this is the case, all of the data is prefetched and count() is manually called on the - resulting array in order to achieve the same functionality. + resulting array in order to achieve the same methodality. -$query->num_fields() -==================== +**$query->num_fields()** The number of FIELDS (columns) returned by the query. Make sure to call -the function using your query result object:: +the method using your query result object:: $query = $this->db->query('SELECT * FROM my_table'); echo $query->num_fields(); -$query->free_result() -===================== +**$query->free_result()** It frees the memory associated with the result and deletes the result resource ID. Normally PHP frees its memory automatically at the end of @@ -217,8 +223,7 @@ Example:: echo $row->name; $query2->free_result(); // The $query2 result object will no longer be available -data_seek() -=========== +**data_seek()** This method sets the internal pointer for the next result row to be fetched. It is only useful in combination with ``unbuffered_row()``. |