blob: 90f49afb14ea2d12e7bfcab2785edeb1bd6d1d3f [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001#######
2Queries
3#######
4
5$this->db->query();
6===================
7
8To submit a query, use the following function::
9
10 $this->db->query('YOUR QUERY HERE');
11
12The query() function returns a database result **object** when "read"
13type queries are run, which you can use to :doc:`show your
14results <results>`. When "write" type queries are run it simply
15returns TRUE or FALSE depending on success or failure. When retrieving
16data you will typically assign the query to your own variable, like
17this::
18
19 $query = $this->db->query('YOUR QUERY HERE');
20
21$this->db->simple_query();
Andrey Andreev8c833f42014-05-14 13:32:34 +030022==========================
Derek Jones8ede1a22011-10-05 13:34:52 -050023
Andrey Andreev1922a882012-06-15 15:16:51 +030024This is a simplified version of the $this->db->query() method. It DOES
25NOT return a database result set, nor does it set the query timer, or
26compile bind data, or store your query for debugging. It simply lets you
27submit a query. Most users will rarely use this function.
28
29It returns whatever the database drivers' "execute" function returns.
30That typically is TRUE/FALSE on success or failure for write type queries
31such as INSERT, DELETE or UPDATE statements (which is what it really
32should be used for) and a resource/object on success for queries with
33fetchable results.
34
35::
36
37 if ($this->db->simple_query('YOUR QUERY'))
38 {
39 echo "Success!";
40 }
41 else
42 {
43 echo "Query failed!";
44 }
45
Andrey Andreev8c833f42014-05-14 13:32:34 +030046.. note:: PostgreSQL's ``pg_exec()`` function (for example) always
47 returns a resource on success, even for write type queries.
48 So take that in mind if you're looking for a boolean value.
Derek Jones8ede1a22011-10-05 13:34:52 -050049
50***************************************
51Working with Database prefixes manually
52***************************************
53
54If you have configured a database prefix and would like to prepend it to
55a table name for use in a native SQL query for example, then you can use
56the following::
57
58 $this->db->dbprefix('tablename'); // outputs prefix_tablename
59
60
61If for any reason you would like to change the prefix programatically
62without needing to create a new connection, you can use this method::
63
Joseph Wensleyf24f4042011-10-06 22:53:29 -040064 $this->db->set_dbprefix('newprefix');
65 $this->db->dbprefix('tablename'); // outputs newprefix_tablename
Derek Jones8ede1a22011-10-05 13:34:52 -050066
67
68**********************
69Protecting identifiers
70**********************
71
72In many databases it is advisable to protect table and field names - for
Jamie Rumbelow7efad202012-02-19 12:37:00 +000073example with backticks in MySQL. **Query Builder queries are
Derek Jones8ede1a22011-10-05 13:34:52 -050074automatically protected**, however if you need to manually protect an
75identifier you can use::
76
77 $this->db->protect_identifiers('table_name');
78
Andrey Andreev8c833f42014-05-14 13:32:34 +030079.. important:: Although the Query Builder will try its best to properly
80 quote any field and table names that you feed it, note that it
81 is NOT designed to work with arbitrary user input. DO NOT feed it
82 with unsanitized user data.
Derek Jones8ede1a22011-10-05 13:34:52 -050083
84This function will also add a table prefix to your table, assuming you
85have a prefix specified in your database config file. To enable the
Kit Sundeed2f95c2014-05-15 16:20:19 +080086prefixing set TRUE (boolean) via the second parameter::
Derek Jones8ede1a22011-10-05 13:34:52 -050087
88 $this->db->protect_identifiers('table_name', TRUE);
89
90
91****************
92Escaping Queries
93****************
94
95It's a very good security practice to escape your data before submitting
96it into your database. CodeIgniter has three methods that help you do
97this:
98
99#. **$this->db->escape()** This function determines the data type so
100 that it can escape only string data. It also automatically adds
101 single quotes around the data so you don't have to:
102 ::
103
104 $sql = "INSERT INTO table (title) VALUES(".$this->db->escape($title).")";
105
106#. **$this->db->escape_str()** This function escapes the data passed to
107 it, regardless of type. Most of the time you'll use the above
108 function rather than this one. Use the function like this:
109 ::
110
111 $sql = "INSERT INTO table (title) VALUES('".$this->db->escape_str($title)."')";
112
113#. **$this->db->escape_like_str()** This method should be used when
114 strings are to be used in LIKE conditions so that LIKE wildcards
115 ('%', '\_') in the string are also properly escaped.
116
117::
118
119 $search = '20% raise'; $sql = "SELECT id FROM table WHERE column LIKE '%".$this->db->escape_like_str($search)."%'";
120
121
122**************
123Query Bindings
124**************
125
126Bindings enable you to simplify your query syntax by letting the system
127put the queries together for you. Consider the following example::
128
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400129 $sql = "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?";
130 $this->db->query($sql, array(3, 'live', 'Rick'));
Derek Jones8ede1a22011-10-05 13:34:52 -0500131
132The question marks in the query are automatically replaced with the
133values in the array in the second parameter of the query function.
134
135The secondary benefit of using binds is that the values are
136automatically escaped, producing safer queries. You don't have to
137remember to manually escape data; the engine does it automatically for
138you.
Andrey Andreev4be5de12012-03-02 15:45:41 +0200139
140***************
141Handling Errors
142***************
143
144$this->db->error();
145===================
146
147If you need to get the last error that has occured, the error() method
148will return an array containing its code and message. Here's a quick
149example::
150
151 if ( ! $this->db->simple_query('SELECT `example_field` FROM `example_table`'))
152 {
153 $error = $this->db->error(); // Has keys 'code' and 'message'
154 }
155