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 | |
| 31 | echo $this->db->count_all('my_table'); // Produces an integer, like 25 |
| 32 | |
| 33 | $this->db->platform() |
| 34 | ===================== |
| 35 | |
| 36 | Outputs the database platform you are running (MySQL, MS SQL, Postgres, |
| 37 | etc...):: |
| 38 | |
| 39 | echo $this->db->platform(); |
| 40 | |
| 41 | $this->db->version() |
| 42 | ==================== |
| 43 | |
| 44 | Outputs the database version you are running:: |
| 45 | |
| 46 | echo $this->db->version(); |
| 47 | |
| 48 | $this->db->last_query(); |
| 49 | ========================= |
| 50 | |
| 51 | Returns the last query that was run (the query string, not the result). |
| 52 | Example:: |
| 53 | |
| 54 | $str = $this->db->last_query(); // Produces: SELECT * FROM sometable.... |
| 55 | |
| 56 | The following two functions help simplify the process of writing |
| 57 | database INSERTs and UPDATEs. |
| 58 | |
| 59 | $this->db->insert_string(); |
| 60 | ============================ |
| 61 | |
| 62 | This function simplifies the process of writing database inserts. It |
| 63 | returns a correctly formatted SQL insert string. Example:: |
| 64 | |
| 65 | $data = array('name' => $name, 'email' => $email, 'url' => $url); $str = $this->db->insert_string('table_name', $data); |
| 66 | |
| 67 | The first parameter is the table name, the second is an associative |
| 68 | array with the data to be inserted. The above example produces:: |
| 69 | |
| 70 | INSERT INTO table_name (name, email, url) VALUES ('Rick', 'rick@example.com', 'example.com') |
| 71 | |
| 72 | Note: Values are automatically escaped, producing safer queries. |
| 73 | |
| 74 | $this->db->update_string(); |
| 75 | ============================ |
| 76 | |
| 77 | This function simplifies the process of writing database updates. It |
| 78 | returns a correctly formatted SQL update string. Example:: |
| 79 | |
| 80 | $data = array('name' => $name, 'email' => $email, 'url' => $url); $where = "author_id = 1 AND status = 'active'"; $str = $this->db->update_string('table_name', $data, $where); |
| 81 | |
| 82 | The first parameter is the table name, the second is an associative |
| 83 | array with the data to be updated, and the third parameter is the |
| 84 | "where" clause. The above example produces:: |
| 85 | |
| 86 | UPDATE table_name SET name = 'Rick', email = 'rick@example.com', url = 'example.com' WHERE author_id = 1 AND status = 'active' |
| 87 | |
| 88 | Note: Values are automatically escaped, producing safer queries. |