blob: 2d997a9e0d3f39eb091cb6c2a72f83f2c315f18a [file] [log] [blame]
James L Parryb4f8caa2014-11-24 17:10:31 -08001####################
2Query Helper Methods
3####################
Derek Jones8ede1a22011-10-05 13:34:52 -05004
James L Parryb4f8caa2014-11-24 17:10:31 -08005Information From Executing a Query
6==================================
7
8**$this->db->insert_id()**
Derek Jones8ede1a22011-10-05 13:34:52 -05009
10The insert ID number when performing database inserts.
11
Timothy Warren6e821ce2012-02-15 18:40:39 -050012.. 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 Jones8ede1a22011-10-05 13:34:52 -050015
James L Parryb4f8caa2014-11-24 17:10:31 -080016**$this->db->affected_rows()**
Derek Jones8ede1a22011-10-05 13:34:52 -050017
18Displays 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 Parryb4f8caa2014-11-24 17:10:31 -080026**$this->db->last_query()**
Derek Jones8ede1a22011-10-05 13:34:52 -050027
28Returns the last query that was run (the query string, not the result).
29Example::
30
Joseph Wensleyf24f4042011-10-06 22:53:29 -040031 $str = $this->db->last_query();
32
33 // Produces: SELECT * FROM sometable....
Derek Jones8ede1a22011-10-05 13:34:52 -050034
Derek Jones8ede1a22011-10-05 13:34:52 -050035
Andrey Andreev1c8245a2014-01-20 10:28:20 +020036.. note:: Disabling the **save_queries** setting in your database
37 configuration will render this function useless.
38
James L Parryb4f8caa2014-11-24 17:10:31 -080039Information About Your Database
40===============================
41
42**$this->db->count_all()**
43
44Permits you to determine the number of rows in a particular table.
45Submit 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
53Outputs the database platform you are running (MySQL, MS SQL, Postgres,
54etc...)::
55
56 echo $this->db->platform();
57
58**$this->db->version()**
59
60Outputs the database version you are running::
61
62 echo $this->db->version();
63
64Making Your Queries Easier
Andrey Andreev1c8245a2014-01-20 10:28:20 +020065==========================
Derek Jones8ede1a22011-10-05 13:34:52 -050066
James L Parryb4f8caa2014-11-24 17:10:31 -080067**$this->db->insert_string()**
68
Derek Jones8ede1a22011-10-05 13:34:52 -050069This function simplifies the process of writing database inserts. It
70returns a correctly formatted SQL insert string. Example::
71
Joseph Wensleyf24f4042011-10-06 22:53:29 -040072 $data = array('name' => $name, 'email' => $email, 'url' => $url);
73
74 $str = $this->db->insert_string('table_name', $data);
Derek Jones8ede1a22011-10-05 13:34:52 -050075
76The first parameter is the table name, the second is an associative
77array 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 Wensleyf24f4042011-10-06 22:53:29 -040081.. note:: Values are automatically escaped, producing safer queries.
Derek Jones8ede1a22011-10-05 13:34:52 -050082
James L Parryb4f8caa2014-11-24 17:10:31 -080083**$this->db->update_string()**
Derek Jones8ede1a22011-10-05 13:34:52 -050084
85This function simplifies the process of writing database updates. It
86returns a correctly formatted SQL update string. Example::
87
Joseph Wensleyf24f4042011-10-06 22:53:29 -040088 $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 Jones8ede1a22011-10-05 13:34:52 -050093
94The first parameter is the table name, the second is an associative
95array 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 Andreev1c8245a2014-01-20 10:28:20 +0200100.. note:: Values are automatically escaped, producing safer queries.