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(); |
| 22 | =========================== |
| 23 | |
| 24 | This is a simplified version of the $this->db->query() function. It ONLY |
| 25 | returns TRUE/FALSE on success or failure. It DOES NOT return a database |
| 26 | result set, nor does it set the query timer, or compile bind data, or |
| 27 | store your query for debugging. It simply lets you submit a query. Most |
| 28 | users will rarely use this function. |
| 29 | |
| 30 | *************************************** |
| 31 | Working with Database prefixes manually |
| 32 | *************************************** |
| 33 | |
| 34 | If you have configured a database prefix and would like to prepend it to |
| 35 | a table name for use in a native SQL query for example, then you can use |
| 36 | the following:: |
| 37 | |
| 38 | $this->db->dbprefix('tablename'); // outputs prefix_tablename |
| 39 | |
| 40 | |
| 41 | If for any reason you would like to change the prefix programatically |
| 42 | without needing to create a new connection, you can use this method:: |
| 43 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 44 | $this->db->set_dbprefix('newprefix'); |
| 45 | $this->db->dbprefix('tablename'); // outputs newprefix_tablename |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 46 | |
| 47 | |
| 48 | ********************** |
| 49 | Protecting identifiers |
| 50 | ********************** |
| 51 | |
| 52 | In many databases it is advisable to protect table and field names - for |
| 53 | example with backticks in MySQL. **Active Record queries are |
| 54 | automatically protected**, however if you need to manually protect an |
| 55 | identifier you can use:: |
| 56 | |
| 57 | $this->db->protect_identifiers('table_name'); |
| 58 | |
| 59 | |
| 60 | This function will also add a table prefix to your table, assuming you |
| 61 | have a prefix specified in your database config file. To enable the |
| 62 | prefixing set TRUE (boolen) via the second parameter:: |
| 63 | |
| 64 | $this->db->protect_identifiers('table_name', TRUE); |
| 65 | |
| 66 | |
| 67 | **************** |
| 68 | Escaping Queries |
| 69 | **************** |
| 70 | |
| 71 | It's a very good security practice to escape your data before submitting |
| 72 | it into your database. CodeIgniter has three methods that help you do |
| 73 | this: |
| 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 | ************** |
| 99 | Query Bindings |
| 100 | ************** |
| 101 | |
| 102 | Bindings enable you to simplify your query syntax by letting the system |
| 103 | put the queries together for you. Consider the following example:: |
| 104 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 105 | $sql = "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?"; |
| 106 | $this->db->query($sql, array(3, 'live', 'Rick')); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 107 | |
| 108 | The question marks in the query are automatically replaced with the |
| 109 | values in the array in the second parameter of the query function. |
| 110 | |
| 111 | The secondary benefit of using binds is that the values are |
| 112 | automatically escaped, producing safer queries. You don't have to |
| 113 | remember to manually escape data; the engine does it automatically for |
| 114 | you. |