Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ################################## |
| 2 | Database Quick Start: Example Code |
| 3 | ################################## |
| 4 | |
| 5 | The following page contains example code showing how the database class |
| 6 | is used. For complete details please read the individual pages |
| 7 | describing each function. |
| 8 | |
| 9 | Initializing the Database Class |
| 10 | =============================== |
| 11 | |
| 12 | The following code loads and initializes the database class based on |
| 13 | your :doc:`configuration <configuration>` settings:: |
| 14 | |
| 15 | $this->load->database(); |
| 16 | |
| 17 | Once loaded the class is ready to be used as described below. |
| 18 | |
| 19 | Note: If all your pages require database access you can connect |
| 20 | automatically. See the :doc:`connecting <connecting>` page for details. |
| 21 | |
| 22 | Standard Query With Multiple Results (Object Version) |
| 23 | ===================================================== |
| 24 | |
| 25 | :: |
| 26 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame^] | 27 | $query = $this->db->query('SELECT name, title, email FROM my_table'); |
| 28 | |
| 29 | foreach ($query->result() as $row) |
| 30 | { |
| 31 | echo $row->title; |
| 32 | echo $row->name; |
| 33 | echo $row->email; |
| 34 | } |
| 35 | |
| 36 | echo 'Total Results: ' . $query->num_rows(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 37 | |
| 38 | The above result() function returns an array of **objects**. Example: |
| 39 | $row->title |
| 40 | |
| 41 | Standard Query With Multiple Results (Array Version) |
| 42 | ==================================================== |
| 43 | |
| 44 | :: |
| 45 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame^] | 46 | $query = $this->db->query('SELECT name, title, email FROM my_table'); |
| 47 | |
| 48 | foreach ($query->result_array() as $row) |
| 49 | { |
| 50 | echo $row['title']; |
| 51 | echo $row['name']; |
| 52 | echo $row['email']; |
| 53 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 54 | |
| 55 | The above result_array() function returns an array of standard array |
| 56 | indexes. Example: $row['title'] |
| 57 | |
| 58 | Testing for Results |
| 59 | =================== |
| 60 | |
| 61 | If you run queries that might **not** produce a result, you are |
| 62 | encouraged to test for a result first using the num_rows() function:: |
| 63 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame^] | 64 | $query = $this->db->query("YOUR QUERY"); |
| 65 | if ($query->num_rows() > 0) |
| 66 | { |
| 67 | foreach ($query->result() as $row) |
| 68 | { |
| 69 | echo $row->title; |
| 70 | echo $row->name; |
| 71 | echo $row->body; |
| 72 | } |
| 73 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 74 | |
| 75 | Standard Query With Single Result |
| 76 | ================================= |
| 77 | |
| 78 | :: |
| 79 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame^] | 80 | $query = $this->db->query('SELECT name FROM my_table LIMIT 1'); |
| 81 | $row = $query->row(); |
| 82 | echo $row->name; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 83 | |
| 84 | The above row() function returns an **object**. Example: $row->name |
| 85 | |
| 86 | Standard Query With Single Result (Array version) |
| 87 | ================================================= |
| 88 | |
| 89 | :: |
| 90 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame^] | 91 | $query = $this->db->query('SELECT name FROM my_table LIMIT 1'); |
| 92 | $row = $query->row_array(); |
| 93 | echo $row['name']; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 94 | |
| 95 | The above row_array() function returns an **array**. Example: |
| 96 | $row['name'] |
| 97 | |
| 98 | Standard Insert |
| 99 | =============== |
| 100 | |
| 101 | :: |
| 102 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame^] | 103 | $sql = "INSERT INTO mytable (title, name) VALUES (".$this->db->escape($title).", ".$this->db->escape($name).")"; |
| 104 | $this->db->query($sql); |
| 105 | echo $this->db->affected_rows(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 106 | |
| 107 | Active Record Query |
| 108 | =================== |
| 109 | |
| 110 | The :doc:`Active Record Pattern <active_record>` gives you a simplified |
| 111 | means of retrieving data:: |
| 112 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame^] | 113 | $query = $this->db->get('table_name'); |
| 114 | |
| 115 | foreach ($query->result() as $row) |
| 116 | { |
| 117 | echo $row->title; |
| 118 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 119 | |
| 120 | The above get() function retrieves all the results from the supplied |
| 121 | table. The :doc:`Active Record <active_record>` class contains a full |
| 122 | compliment of functions for working with data. |
| 123 | |
| 124 | Active Record Insert |
| 125 | ==================== |
| 126 | |
| 127 | :: |
| 128 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame^] | 129 | $data = array( |
| 130 | 'title' => $title, |
| 131 | 'name' => $name, |
| 132 | 'date' => $date |
| 133 | ); |
| 134 | |
| 135 | $this->db->insert('mytable', $data); // Produces: INSERT INTO mytable (title, name, date) VALUES ('{$title}', '{$name}', '{$date}') |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 136 | |