From 3fc30aacd184d7d0ead78d154894412aaea4e577 Mon Sep 17 00:00:00 2001 From: James L Parry Date: Mon, 24 Nov 2014 16:12:52 -0800 Subject: Database guide update Changed the name on the TOC page to match the main user guide TOC. Signed-off-by:James L Parry --- user_guide_src/source/database/index.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'user_guide_src/source/database') 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 -- cgit v1.2.3-24-g4f1b From d980e3307b9919b239d4edb307f18c287cac8dd0 Mon Sep 17 00:00:00 2001 From: James L Parry Date: Mon, 24 Nov 2014 16:16:40 -0800 Subject: Database guide update Updated the queries page, making heading levels consistent with each other. Signed-off-by:James L Parry --- user_guide_src/source/database/queries.rst | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'user_guide_src/source/database') diff --git a/user_guide_src/source/database/queries.rst b/user_guide_src/source/database/queries.rst index 76ff1083f..d775d20c4 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. @@ -150,8 +155,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 -- cgit v1.2.3-24-g4f1b From e4a9f644b6cf173cec7ea01bfecab21760777858 Mon Sep 17 00:00:00 2001 From: James L Parry Date: Mon, 24 Nov 2014 16:20:53 -0800 Subject: Database guide update Updated the results writeup. The formatting was inconsistent -> fixed. The terminology was inconsistent, often calling a method a function -> fixed. Signed-off-by:James L Parry --- user_guide_src/source/database/results.rst | 34 +++++++++++++----------------- 1 file changed, 15 insertions(+), 19 deletions(-) (limited to 'user_guide_src/source/database') diff --git a/user_guide_src/source/database/results.rst b/user_guide_src/source/database/results.rst index e0a87a851..c52fe9b19 100644 --- a/user_guide_src/source/database/results.rst +++ b/user_guide_src/source/database/results.rst @@ -7,7 +7,7 @@ There are several ways to generate query results: 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 +20,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 +53,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:: @@ -69,7 +69,7 @@ loop, like this:: 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 +101,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 +136,7 @@ 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 +163,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 +180,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 +214,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()``. -- cgit v1.2.3-24-g4f1b From b4f8caac35976b8938a996f6f2ed9a65c1c158a1 Mon Sep 17 00:00:00 2001 From: James L Parry Date: Mon, 24 Nov 2014 17:10:31 -0800 Subject: Database guide update Updated the helpers writeup, making the format more consistene with the rest of the database writeups. Signed-off-by:James L Parry --- user_guide_src/source/database/helpers.rst | 61 +++++++++++++++--------------- 1 file changed, 31 insertions(+), 30 deletions(-) (limited to 'user_guide_src/source/database') 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:: -- cgit v1.2.3-24-g4f1b From 014bc89142a576d056e1dd025bb703d27262337c Mon Sep 17 00:00:00 2001 From: James L Parry Date: Mon, 24 Nov 2014 17:14:19 -0800 Subject: Database guide update Fixed the structure of the results page.. Signed-off-by:James L Parry --- user_guide_src/source/database/results.rst | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'user_guide_src/source/database') diff --git a/user_guide_src/source/database/results.rst b/user_guide_src/source/database/results.rst index c52fe9b19..9f1d60ed5 100644 --- a/user_guide_src/source/database/results.rst +++ b/user_guide_src/source/database/results.rst @@ -4,6 +4,10 @@ Generating Query Results There are several ways to generate query results: +************* +Result Arrays +************* + result() ======== @@ -66,6 +70,10 @@ loop, like this:: echo $row['body']; } +*********** +Result Rows +*********** + row() ===== -- cgit v1.2.3-24-g4f1b From 8252b75630e27ed92d677cc30a2c19d5096a3d4c Mon Sep 17 00:00:00 2001 From: James L Parry Date: Mon, 24 Nov 2014 17:26:58 -0800 Subject: Database guide update Minor change to try to keep line lengths reasonable. Awkward with the example SQL queries. Signed-off-by:James L Parry --- user_guide_src/source/database/queries.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'user_guide_src/source/database') diff --git a/user_guide_src/source/database/queries.rst b/user_guide_src/source/database/queries.rst index d775d20c4..43a0a30bf 100644 --- a/user_guide_src/source/database/queries.rst +++ b/user_guide_src/source/database/queries.rst @@ -121,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)."%'"; ************** -- cgit v1.2.3-24-g4f1b From 8b85526c6a9758ef92340ba0df6a32249a578b39 Mon Sep 17 00:00:00 2001 From: James L Parry Date: Mon, 24 Nov 2014 17:29:04 -0800 Subject: Database guide update Minor change to try to keep line lengths reasonable. Awkward with the example SQL queries. Signed-off-by:James L Parry --- user_guide_src/source/database/results.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'user_guide_src/source/database') diff --git a/user_guide_src/source/database/results.rst b/user_guide_src/source/database/results.rst index 9f1d60ed5..e06985130 100644 --- a/user_guide_src/source/database/results.rst +++ b/user_guide_src/source/database/results.rst @@ -144,7 +144,8 @@ parameter: | **$row = $query->next_row('array')** | **$row = $query->previous_row('array')** -.. note:: all the methods 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() ================ -- cgit v1.2.3-24-g4f1b