James L Parry | b4f8caa | 2014-11-24 17:10:31 -0800 | [diff] [blame] | 1 | #################### |
| 2 | Query Helper Methods |
| 3 | #################### |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 4 | |
James L Parry | b4f8caa | 2014-11-24 17:10:31 -0800 | [diff] [blame] | 5 | Information From Executing a Query |
| 6 | ================================== |
| 7 | |
| 8 | **$this->db->insert_id()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 9 | |
| 10 | The insert ID number when performing database inserts. |
| 11 | |
Timothy Warren | 6e821ce | 2012-02-15 18:40:39 -0500 | [diff] [blame] | 12 | .. note:: If using the PDO driver with PostgreSQL, or using the Interbase |
| 13 | driver, this function requires a $name parameter, which specifies the |
| 14 | appropriate sequence to check for the insert id. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 15 | |
James L Parry | b4f8caa | 2014-11-24 17:10:31 -0800 | [diff] [blame] | 16 | **$this->db->affected_rows()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 17 | |
| 18 | Displays the number of affected rows, when doing "write" type queries |
| 19 | (insert, update, etc.). |
| 20 | |
| 21 | .. note:: In MySQL "DELETE FROM TABLE" returns 0 affected rows. The database |
| 22 | class has a small hack that allows it to return the correct number of |
| 23 | affected rows. By default this hack is enabled but it can be turned off |
| 24 | in the database driver file. |
| 25 | |
James L Parry | b4f8caa | 2014-11-24 17:10:31 -0800 | [diff] [blame] | 26 | **$this->db->last_query()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 27 | |
| 28 | Returns the last query that was run (the query string, not the result). |
| 29 | Example:: |
| 30 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 31 | $str = $this->db->last_query(); |
| 32 | |
| 33 | // Produces: SELECT * FROM sometable.... |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 34 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 35 | |
Andrey Andreev | 1c8245a | 2014-01-20 10:28:20 +0200 | [diff] [blame] | 36 | .. note:: Disabling the **save_queries** setting in your database |
| 37 | configuration will render this function useless. |
| 38 | |
James L Parry | b4f8caa | 2014-11-24 17:10:31 -0800 | [diff] [blame] | 39 | Information About Your Database |
| 40 | =============================== |
| 41 | |
| 42 | **$this->db->count_all()** |
| 43 | |
| 44 | Permits you to determine the number of rows in a particular table. |
| 45 | Submit the table name in the first parameter. Example:: |
| 46 | |
| 47 | echo $this->db->count_all('my_table'); |
| 48 | |
| 49 | // Produces an integer, like 25 |
| 50 | |
| 51 | **$this->db->platform()** |
| 52 | |
| 53 | Outputs the database platform you are running (MySQL, MS SQL, Postgres, |
| 54 | etc...):: |
| 55 | |
| 56 | echo $this->db->platform(); |
| 57 | |
| 58 | **$this->db->version()** |
| 59 | |
| 60 | Outputs the database version you are running:: |
| 61 | |
| 62 | echo $this->db->version(); |
| 63 | |
| 64 | Making Your Queries Easier |
Andrey Andreev | 1c8245a | 2014-01-20 10:28:20 +0200 | [diff] [blame] | 65 | ========================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 66 | |
James L Parry | b4f8caa | 2014-11-24 17:10:31 -0800 | [diff] [blame] | 67 | **$this->db->insert_string()** |
| 68 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 69 | This function simplifies the process of writing database inserts. It |
| 70 | returns a correctly formatted SQL insert string. Example:: |
| 71 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 72 | $data = array('name' => $name, 'email' => $email, 'url' => $url); |
| 73 | |
| 74 | $str = $this->db->insert_string('table_name', $data); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 75 | |
| 76 | The first parameter is the table name, the second is an associative |
| 77 | array with the data to be inserted. The above example produces:: |
| 78 | |
| 79 | INSERT INTO table_name (name, email, url) VALUES ('Rick', 'rick@example.com', 'example.com') |
| 80 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 81 | .. note:: Values are automatically escaped, producing safer queries. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 82 | |
James L Parry | b4f8caa | 2014-11-24 17:10:31 -0800 | [diff] [blame] | 83 | **$this->db->update_string()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 84 | |
| 85 | This function simplifies the process of writing database updates. It |
| 86 | returns a correctly formatted SQL update string. Example:: |
| 87 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 88 | $data = array('name' => $name, 'email' => $email, 'url' => $url); |
| 89 | |
| 90 | $where = "author_id = 1 AND status = 'active'"; |
| 91 | |
| 92 | $str = $this->db->update_string('table_name', $data, $where); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 93 | |
| 94 | The first parameter is the table name, the second is an associative |
| 95 | array with the data to be updated, and the third parameter is the |
| 96 | "where" clause. The above example produces:: |
| 97 | |
| 98 | UPDATE table_name SET name = 'Rick', email = 'rick@example.com', url = 'example.com' WHERE author_id = 1 AND status = 'active' |
| 99 | |
Andrey Andreev | 1c8245a | 2014-01-20 10:28:20 +0200 | [diff] [blame] | 100 | .. note:: Values are automatically escaped, producing safer queries. |