blob: 7ea19e9f664574b610ea8241f4691d4458646585 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001######################
2Query Helper Functions
3######################
4
5$this->db->insert_id()
6=======================
7
8The 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
17Displays 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
28Permits you to determine the number of rows in a particular table.
29Submit the table name in the first parameter. Example::
30
Joseph Wensleyf24f4042011-10-06 22:53:29 -040031 echo $this->db->count_all('my_table');
32
33 // Produces an integer, like 25
Derek Jones8ede1a22011-10-05 13:34:52 -050034
35$this->db->platform()
36=====================
37
38Outputs the database platform you are running (MySQL, MS SQL, Postgres,
39etc...)::
40
41 echo $this->db->platform();
42
43$this->db->version()
44====================
45
46Outputs the database version you are running::
47
48 echo $this->db->version();
49
50$this->db->last_query();
51=========================
52
53Returns the last query that was run (the query string, not the result).
54Example::
55
Joseph Wensleyf24f4042011-10-06 22:53:29 -040056 $str = $this->db->last_query();
57
58 // Produces: SELECT * FROM sometable....
Derek Jones8ede1a22011-10-05 13:34:52 -050059
60The following two functions help simplify the process of writing
61database INSERTs and UPDATEs.
62
63$this->db->insert_string();
64============================
65
66This function simplifies the process of writing database inserts. It
67returns a correctly formatted SQL insert string. Example::
68
Joseph Wensleyf24f4042011-10-06 22:53:29 -040069 $data = array('name' => $name, 'email' => $email, 'url' => $url);
70
71 $str = $this->db->insert_string('table_name', $data);
Derek Jones8ede1a22011-10-05 13:34:52 -050072
73The first parameter is the table name, the second is an associative
74array 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 Wensleyf24f4042011-10-06 22:53:29 -040078.. note:: Values are automatically escaped, producing safer queries.
Derek Jones8ede1a22011-10-05 13:34:52 -050079
80$this->db->update_string();
81============================
82
83This function simplifies the process of writing database updates. It
84returns a correctly formatted SQL update string. Example::
85
Joseph Wensleyf24f4042011-10-06 22:53:29 -040086 $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 Jones8ede1a22011-10-05 13:34:52 -050091
92The first parameter is the table name, the second is an associative
93array 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 Wensleyf24f4042011-10-06 22:53:29 -040098.. note:: Values are automatically escaped, producing safer queries.