Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ####### |
| 2 | Queries |
| 3 | ####### |
| 4 | |
James L Parry | d980e33 | 2014-11-24 16:16:40 -0800 | [diff] [blame] | 5 | ************ |
| 6 | Query Basics |
| 7 | ************ |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 8 | |
James L Parry | d980e33 | 2014-11-24 16:16:40 -0800 | [diff] [blame] | 9 | Regular Queries |
| 10 | =============== |
| 11 | |
| 12 | To submit a query, use the **query** function:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 13 | |
| 14 | $this->db->query('YOUR QUERY HERE'); |
| 15 | |
| 16 | The query() function returns a database result **object** when "read" |
| 17 | type queries are run, which you can use to :doc:`show your |
| 18 | results <results>`. When "write" type queries are run it simply |
| 19 | returns TRUE or FALSE depending on success or failure. When retrieving |
| 20 | data you will typically assign the query to your own variable, like |
| 21 | this:: |
| 22 | |
| 23 | $query = $this->db->query('YOUR QUERY HERE'); |
| 24 | |
James L Parry | d980e33 | 2014-11-24 16:16:40 -0800 | [diff] [blame] | 25 | Simplified Queries |
| 26 | ================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 27 | |
James L Parry | d980e33 | 2014-11-24 16:16:40 -0800 | [diff] [blame] | 28 | The **simple_query** method is a simplified version of the |
| 29 | $this->db->query() method. It DOES |
Andrey Andreev | 1922a88 | 2012-06-15 15:16:51 +0300 | [diff] [blame] | 30 | NOT return a database result set, nor does it set the query timer, or |
| 31 | compile bind data, or store your query for debugging. It simply lets you |
| 32 | submit a query. Most users will rarely use this function. |
| 33 | |
| 34 | It returns whatever the database drivers' "execute" function returns. |
| 35 | That typically is TRUE/FALSE on success or failure for write type queries |
| 36 | such as INSERT, DELETE or UPDATE statements (which is what it really |
| 37 | should be used for) and a resource/object on success for queries with |
| 38 | fetchable results. |
| 39 | |
| 40 | :: |
| 41 | |
| 42 | if ($this->db->simple_query('YOUR QUERY')) |
| 43 | { |
| 44 | echo "Success!"; |
| 45 | } |
| 46 | else |
| 47 | { |
| 48 | echo "Query failed!"; |
| 49 | } |
| 50 | |
Andrey Andreev | 8c833f4 | 2014-05-14 13:32:34 +0300 | [diff] [blame] | 51 | .. note:: PostgreSQL's ``pg_exec()`` function (for example) always |
| 52 | returns a resource on success, even for write type queries. |
| 53 | So take that in mind if you're looking for a boolean value. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 54 | |
| 55 | *************************************** |
| 56 | Working with Database prefixes manually |
| 57 | *************************************** |
| 58 | |
| 59 | If you have configured a database prefix and would like to prepend it to |
| 60 | a table name for use in a native SQL query for example, then you can use |
| 61 | the following:: |
| 62 | |
| 63 | $this->db->dbprefix('tablename'); // outputs prefix_tablename |
| 64 | |
| 65 | |
| 66 | If for any reason you would like to change the prefix programatically |
| 67 | without needing to create a new connection, you can use this method:: |
| 68 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 69 | $this->db->set_dbprefix('newprefix'); |
| 70 | $this->db->dbprefix('tablename'); // outputs newprefix_tablename |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 71 | |
| 72 | |
| 73 | ********************** |
| 74 | Protecting identifiers |
| 75 | ********************** |
| 76 | |
| 77 | In many databases it is advisable to protect table and field names - for |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 78 | example with backticks in MySQL. **Query Builder queries are |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 79 | automatically protected**, however if you need to manually protect an |
| 80 | identifier you can use:: |
| 81 | |
| 82 | $this->db->protect_identifiers('table_name'); |
| 83 | |
Andrey Andreev | 8c833f4 | 2014-05-14 13:32:34 +0300 | [diff] [blame] | 84 | .. important:: Although the Query Builder will try its best to properly |
| 85 | quote any field and table names that you feed it, note that it |
| 86 | is NOT designed to work with arbitrary user input. DO NOT feed it |
| 87 | with unsanitized user data. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 88 | |
| 89 | This function will also add a table prefix to your table, assuming you |
| 90 | have a prefix specified in your database config file. To enable the |
Kit Sunde | ed2f95c | 2014-05-15 16:20:19 +0800 | [diff] [blame] | 91 | prefixing set TRUE (boolean) via the second parameter:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 92 | |
| 93 | $this->db->protect_identifiers('table_name', TRUE); |
| 94 | |
| 95 | |
| 96 | **************** |
| 97 | Escaping Queries |
| 98 | **************** |
| 99 | |
| 100 | It's a very good security practice to escape your data before submitting |
| 101 | it into your database. CodeIgniter has three methods that help you do |
| 102 | this: |
| 103 | |
| 104 | #. **$this->db->escape()** This function determines the data type so |
| 105 | that it can escape only string data. It also automatically adds |
| 106 | single quotes around the data so you don't have to: |
| 107 | :: |
| 108 | |
| 109 | $sql = "INSERT INTO table (title) VALUES(".$this->db->escape($title).")"; |
| 110 | |
| 111 | #. **$this->db->escape_str()** This function escapes the data passed to |
| 112 | it, regardless of type. Most of the time you'll use the above |
| 113 | function rather than this one. Use the function like this: |
| 114 | :: |
| 115 | |
| 116 | $sql = "INSERT INTO table (title) VALUES('".$this->db->escape_str($title)."')"; |
| 117 | |
| 118 | #. **$this->db->escape_like_str()** This method should be used when |
| 119 | strings are to be used in LIKE conditions so that LIKE wildcards |
| 120 | ('%', '\_') in the string are also properly escaped. |
| 121 | |
| 122 | :: |
| 123 | |
James L Parry | 8252b75 | 2014-11-24 17:26:58 -0800 | [diff] [blame] | 124 | $search = '20% raise'; |
| 125 | $sql = "SELECT id FROM table WHERE column LIKE '%" . |
Andrey Andreev | 86d2ec4 | 2016-02-17 19:19:56 +0200 | [diff] [blame] | 126 | $this->db->escape_like_str($search)."%' ESCAPE '!'"; |
| 127 | |
| 128 | .. important:: The ``escape_like_str()`` method uses '!' (exclamation mark) |
| 129 | to escape special characters for *LIKE* conditions. Because this |
| 130 | method escapes partial strings that you would wrap in quotes |
| 131 | yourself, it cannot automatically add the ``ESCAPE '!'`` |
| 132 | condition for you, and so you'll have to manually do that. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 133 | |
| 134 | |
| 135 | ************** |
| 136 | Query Bindings |
| 137 | ************** |
| 138 | |
| 139 | Bindings enable you to simplify your query syntax by letting the system |
| 140 | put the queries together for you. Consider the following example:: |
| 141 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 142 | $sql = "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?"; |
| 143 | $this->db->query($sql, array(3, 'live', 'Rick')); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 144 | |
| 145 | The question marks in the query are automatically replaced with the |
| 146 | values in the array in the second parameter of the query function. |
| 147 | |
clawoo | 1db369f | 2014-10-22 19:10:55 +0300 | [diff] [blame] | 148 | Binding also work with arrays, which will be transformed to IN sets:: |
clawoo | ff8bff1 | 2014-10-21 10:41:02 +0300 | [diff] [blame] | 149 | |
| 150 | $sql = "SELECT * FROM some_table WHERE id IN ? AND status = ? AND author = ?"; |
| 151 | $this->db->query($sql, array(array(3, 6), 'live', 'Rick')); |
| 152 | |
clawoo | 1db369f | 2014-10-22 19:10:55 +0300 | [diff] [blame] | 153 | The resulting query will be:: |
| 154 | |
| 155 | SELECT * FROM some_table WHERE id IN (3,6) AND status = 'live' AND author = 'Rick' |
clawoo | ff8bff1 | 2014-10-21 10:41:02 +0300 | [diff] [blame] | 156 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 157 | The secondary benefit of using binds is that the values are |
| 158 | automatically escaped, producing safer queries. You don't have to |
| 159 | remember to manually escape data; the engine does it automatically for |
| 160 | you. |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 161 | |
| 162 | *************** |
| 163 | Handling Errors |
| 164 | *************** |
| 165 | |
James L Parry | d980e33 | 2014-11-24 16:16:40 -0800 | [diff] [blame] | 166 | **$this->db->error();** |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 167 | |
| 168 | If you need to get the last error that has occured, the error() method |
| 169 | will return an array containing its code and message. Here's a quick |
| 170 | example:: |
| 171 | |
| 172 | if ( ! $this->db->simple_query('SELECT `example_field` FROM `example_table`')) |
| 173 | { |
| 174 | $error = $this->db->error(); // Has keys 'code' and 'message' |
| 175 | } |
| 176 | |