Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ####### |
| 2 | Queries |
| 3 | ####### |
| 4 | |
| 5 | $this->db->query(); |
| 6 | =================== |
| 7 | |
| 8 | To submit a query, use the following function:: |
| 9 | |
| 10 | $this->db->query('YOUR QUERY HERE'); |
| 11 | |
| 12 | The query() function returns a database result **object** when "read" |
| 13 | type queries are run, which you can use to :doc:`show your |
| 14 | results <results>`. When "write" type queries are run it simply |
| 15 | returns TRUE or FALSE depending on success or failure. When retrieving |
| 16 | data you will typically assign the query to your own variable, like |
| 17 | this:: |
| 18 | |
| 19 | $query = $this->db->query('YOUR QUERY HERE'); |
| 20 | |
| 21 | $this->db->simple_query(); |
Andrey Andreev | 8c833f4 | 2014-05-14 13:32:34 +0300 | [diff] [blame] | 22 | ========================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 23 | |
Andrey Andreev | 1922a88 | 2012-06-15 15:16:51 +0300 | [diff] [blame] | 24 | This is a simplified version of the $this->db->query() method. It DOES |
| 25 | NOT return a database result set, nor does it set the query timer, or |
| 26 | compile bind data, or store your query for debugging. It simply lets you |
| 27 | submit a query. Most users will rarely use this function. |
| 28 | |
| 29 | It returns whatever the database drivers' "execute" function returns. |
| 30 | That typically is TRUE/FALSE on success or failure for write type queries |
| 31 | such as INSERT, DELETE or UPDATE statements (which is what it really |
| 32 | should be used for) and a resource/object on success for queries with |
| 33 | fetchable 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 Andreev | 8c833f4 | 2014-05-14 13:32:34 +0300 | [diff] [blame] | 46 | .. 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 49 | |
| 50 | *************************************** |
| 51 | Working with Database prefixes manually |
| 52 | *************************************** |
| 53 | |
| 54 | If you have configured a database prefix and would like to prepend it to |
| 55 | a table name for use in a native SQL query for example, then you can use |
| 56 | the following:: |
| 57 | |
| 58 | $this->db->dbprefix('tablename'); // outputs prefix_tablename |
| 59 | |
| 60 | |
| 61 | If for any reason you would like to change the prefix programatically |
| 62 | without needing to create a new connection, you can use this method:: |
| 63 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 64 | $this->db->set_dbprefix('newprefix'); |
| 65 | $this->db->dbprefix('tablename'); // outputs newprefix_tablename |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 66 | |
| 67 | |
| 68 | ********************** |
| 69 | Protecting identifiers |
| 70 | ********************** |
| 71 | |
| 72 | 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] | 73 | example with backticks in MySQL. **Query Builder queries are |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 74 | automatically protected**, however if you need to manually protect an |
| 75 | identifier you can use:: |
| 76 | |
| 77 | $this->db->protect_identifiers('table_name'); |
| 78 | |
Andrey Andreev | 8c833f4 | 2014-05-14 13:32:34 +0300 | [diff] [blame] | 79 | .. 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 83 | |
| 84 | This function will also add a table prefix to your table, assuming you |
| 85 | have a prefix specified in your database config file. To enable the |
Kit Sunde | ed2f95c | 2014-05-15 16:20:19 +0800 | [diff] [blame] | 86 | prefixing set TRUE (boolean) via the second parameter:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 87 | |
| 88 | $this->db->protect_identifiers('table_name', TRUE); |
| 89 | |
| 90 | |
| 91 | **************** |
| 92 | Escaping Queries |
| 93 | **************** |
| 94 | |
| 95 | It's a very good security practice to escape your data before submitting |
| 96 | it into your database. CodeIgniter has three methods that help you do |
| 97 | this: |
| 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 | ************** |
| 123 | Query Bindings |
| 124 | ************** |
| 125 | |
| 126 | Bindings enable you to simplify your query syntax by letting the system |
| 127 | put the queries together for you. Consider the following example:: |
| 128 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 129 | $sql = "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?"; |
| 130 | $this->db->query($sql, array(3, 'live', 'Rick')); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 131 | |
| 132 | The question marks in the query are automatically replaced with the |
| 133 | values in the array in the second parameter of the query function. |
| 134 | |
| 135 | The secondary benefit of using binds is that the values are |
| 136 | automatically escaped, producing safer queries. You don't have to |
| 137 | remember to manually escape data; the engine does it automatically for |
| 138 | you. |
Andrey Andreev | 4be5de1 | 2012-03-02 15:45:41 +0200 | [diff] [blame] | 139 | |
| 140 | *************** |
| 141 | Handling Errors |
| 142 | *************** |
| 143 | |
| 144 | $this->db->error(); |
| 145 | =================== |
| 146 | |
| 147 | If you need to get the last error that has occured, the error() method |
| 148 | will return an array containing its code and message. Here's a quick |
| 149 | example:: |
| 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 | |