blob: 77bf1b5d2c9dba39efbd440d2d7aa276025a805e [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001######################
2Query Helper Functions
3######################
4
5$this->db->insert_id()
Andrey Andreev1c8245a2014-01-20 10:28:20 +02006======================
Derek Jones8ede1a22011-10-05 13:34:52 -05007
8The insert ID number when performing database inserts.
9
Timothy Warren6e821ce2012-02-15 18:40:39 -050010.. note:: If using the PDO driver with PostgreSQL, or using the Interbase
11 driver, this function requires a $name parameter, which specifies the
12 appropriate sequence to check for the insert id.
Derek Jones8ede1a22011-10-05 13:34:52 -050013
14$this->db->affected_rows()
Andrey Andreev1c8245a2014-01-20 10:28:20 +020015==========================
Derek Jones8ede1a22011-10-05 13:34:52 -050016
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
Andrey Andreev1c8245a2014-01-20 10:28:20 +020025$this->db->count_all()
26======================
Derek Jones8ede1a22011-10-05 13:34:52 -050027
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
Andrey Andreev1c8245a2014-01-20 10:28:20 +020050$this->db->last_query()
51=======================
Derek Jones8ede1a22011-10-05 13:34:52 -050052
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
Derek Jones8ede1a22011-10-05 13:34:52 -050060
Andrey Andreev1c8245a2014-01-20 10:28:20 +020061.. note:: Disabling the **save_queries** setting in your database
62 configuration will render this function useless.
63
64$this->db->insert_string()
65==========================
Derek Jones8ede1a22011-10-05 13:34:52 -050066
67This function simplifies the process of writing database inserts. It
68returns a correctly formatted SQL insert string. Example::
69
Joseph Wensleyf24f4042011-10-06 22:53:29 -040070 $data = array('name' => $name, 'email' => $email, 'url' => $url);
71
72 $str = $this->db->insert_string('table_name', $data);
Derek Jones8ede1a22011-10-05 13:34:52 -050073
74The first parameter is the table name, the second is an associative
75array with the data to be inserted. The above example produces::
76
77 INSERT INTO table_name (name, email, url) VALUES ('Rick', 'rick@example.com', 'example.com')
78
Joseph Wensleyf24f4042011-10-06 22:53:29 -040079.. note:: Values are automatically escaped, producing safer queries.
Derek Jones8ede1a22011-10-05 13:34:52 -050080
Andrey Andreev1c8245a2014-01-20 10:28:20 +020081$this->db->update_string()
82==========================
Derek Jones8ede1a22011-10-05 13:34:52 -050083
84This function simplifies the process of writing database updates. It
85returns a correctly formatted SQL update string. Example::
86
Joseph Wensleyf24f4042011-10-06 22:53:29 -040087 $data = array('name' => $name, 'email' => $email, 'url' => $url);
88
89 $where = "author_id = 1 AND status = 'active'";
90
91 $str = $this->db->update_string('table_name', $data, $where);
Derek Jones8ede1a22011-10-05 13:34:52 -050092
93The first parameter is the table name, the second is an associative
94array with the data to be updated, and the third parameter is the
95"where" clause. The above example produces::
96
97 UPDATE table_name SET name = 'Rick', email = 'rick@example.com', url = 'example.com' WHERE author_id = 1 AND status = 'active'
98
Andrey Andreev1c8245a2014-01-20 10:28:20 +020099.. note:: Values are automatically escaped, producing safer queries.