blob: cfc42c4c332ccf55f3a13daf51517e366d32aab7 [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
44 $this->db->set_dbprefix('newprefix'); $this->db->dbprefix('tablename'); // outputs newprefix_tablename
45
46
47**********************
48Protecting identifiers
49**********************
50
51In many databases it is advisable to protect table and field names - for
52example with backticks in MySQL. **Active Record queries are
53automatically protected**, however if you need to manually protect an
54identifier you can use::
55
56 $this->db->protect_identifiers('table_name');
57
58
59This function will also add a table prefix to your table, assuming you
60have a prefix specified in your database config file. To enable the
61prefixing set TRUE (boolen) via the second parameter::
62
63 $this->db->protect_identifiers('table_name', TRUE);
64
65
66****************
67Escaping Queries
68****************
69
70It's a very good security practice to escape your data before submitting
71it into your database. CodeIgniter has three methods that help you do
72this:
73
74#. **$this->db->escape()** This function determines the data type so
75 that it can escape only string data. It also automatically adds
76 single quotes around the data so you don't have to:
77 ::
78
79 $sql = "INSERT INTO table (title) VALUES(".$this->db->escape($title).")";
80
81#. **$this->db->escape_str()** This function escapes the data passed to
82 it, regardless of type. Most of the time you'll use the above
83 function rather than this one. Use the function like this:
84 ::
85
86 $sql = "INSERT INTO table (title) VALUES('".$this->db->escape_str($title)."')";
87
88#. **$this->db->escape_like_str()** This method should be used when
89 strings are to be used in LIKE conditions so that LIKE wildcards
90 ('%', '\_') in the string are also properly escaped.
91
92::
93
94 $search = '20% raise'; $sql = "SELECT id FROM table WHERE column LIKE '%".$this->db->escape_like_str($search)."%'";
95
96
97**************
98Query Bindings
99**************
100
101Bindings enable you to simplify your query syntax by letting the system
102put the queries together for you. Consider the following example::
103
104 $sql = "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?"; $this->db->query($sql, array(3, 'live', 'Rick'));
105
106The question marks in the query are automatically replaced with the
107values in the array in the second parameter of the query function.
108
109The secondary benefit of using binds is that the values are
110automatically escaped, producing safer queries. You don't have to
111remember to manually escape data; the engine does it automatically for
112you.