blob: 971d5d61dc1081de477e58be9a987941b16078c4 [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();
22===========================
23
24This is a simplified version of the $this->db->query() function. It ONLY
25returns TRUE/FALSE on success or failure. It DOES NOT return a database
26result set, nor does it set the query timer, or compile bind data, or
27store your query for debugging. It simply lets you submit a query. Most
28users will rarely use this function.
29
30***************************************
31Working with Database prefixes manually
32***************************************
33
34If you have configured a database prefix and would like to prepend it to
35a table name for use in a native SQL query for example, then you can use
36the following::
37
38 $this->db->dbprefix('tablename'); // outputs prefix_tablename
39
40
41If for any reason you would like to change the prefix programatically
42without needing to create a new connection, you can use this method::
43
Joseph Wensleyf24f4042011-10-06 22:53:29 -040044 $this->db->set_dbprefix('newprefix');
45 $this->db->dbprefix('tablename'); // outputs newprefix_tablename
Derek Jones8ede1a22011-10-05 13:34:52 -050046
47
48**********************
49Protecting identifiers
50**********************
51
52In many databases it is advisable to protect table and field names - for
53example with backticks in MySQL. **Active Record queries are
54automatically protected**, however if you need to manually protect an
55identifier you can use::
56
57 $this->db->protect_identifiers('table_name');
58
59
60This function will also add a table prefix to your table, assuming you
61have a prefix specified in your database config file. To enable the
62prefixing set TRUE (boolen) via the second parameter::
63
64 $this->db->protect_identifiers('table_name', TRUE);
65
66
67****************
68Escaping Queries
69****************
70
71It's a very good security practice to escape your data before submitting
72it into your database. CodeIgniter has three methods that help you do
73this:
74
75#. **$this->db->escape()** This function determines the data type so
76 that it can escape only string data. It also automatically adds
77 single quotes around the data so you don't have to:
78 ::
79
80 $sql = "INSERT INTO table (title) VALUES(".$this->db->escape($title).")";
81
82#. **$this->db->escape_str()** This function escapes the data passed to
83 it, regardless of type. Most of the time you'll use the above
84 function rather than this one. Use the function like this:
85 ::
86
87 $sql = "INSERT INTO table (title) VALUES('".$this->db->escape_str($title)."')";
88
89#. **$this->db->escape_like_str()** This method should be used when
90 strings are to be used in LIKE conditions so that LIKE wildcards
91 ('%', '\_') in the string are also properly escaped.
92
93::
94
95 $search = '20% raise'; $sql = "SELECT id FROM table WHERE column LIKE '%".$this->db->escape_like_str($search)."%'";
96
97
98**************
99Query Bindings
100**************
101
102Bindings enable you to simplify your query syntax by letting the system
103put the queries together for you. Consider the following example::
104
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400105 $sql = "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?";
106 $this->db->query($sql, array(3, 'live', 'Rick'));
Derek Jones8ede1a22011-10-05 13:34:52 -0500107
108The question marks in the query are automatically replaced with the
109values in the array in the second parameter of the query function.
110
111The secondary benefit of using binds is that the values are
112automatically escaped, producing safer queries. You don't have to
113remember to manually escape data; the engine does it automatically for
114you.