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