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 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 58 | Standard Query With Single Result |
| 59 | ================================= |
| 60 | |
| 61 | :: |
| 62 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 63 | $query = $this->db->query('SELECT name FROM my_table LIMIT 1'); |
| 64 | $row = $query->row(); |
| 65 | echo $row->name; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 66 | |
| 67 | The above row() function returns an **object**. Example: $row->name |
| 68 | |
| 69 | Standard Query With Single Result (Array version) |
| 70 | ================================================= |
| 71 | |
| 72 | :: |
| 73 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 74 | $query = $this->db->query('SELECT name FROM my_table LIMIT 1'); |
| 75 | $row = $query->row_array(); |
| 76 | echo $row['name']; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 77 | |
| 78 | The above row_array() function returns an **array**. Example: |
| 79 | $row['name'] |
| 80 | |
| 81 | Standard Insert |
| 82 | =============== |
| 83 | |
| 84 | :: |
| 85 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 86 | $sql = "INSERT INTO mytable (title, name) VALUES (".$this->db->escape($title).", ".$this->db->escape($name).")"; |
| 87 | $this->db->query($sql); |
| 88 | echo $this->db->affected_rows(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 89 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 90 | Query Builder Query |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 91 | =================== |
| 92 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 93 | The :doc:`Query Builder Pattern <query_builder>` gives you a simplified |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 94 | means of retrieving data:: |
| 95 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 96 | $query = $this->db->get('table_name'); |
| 97 | |
| 98 | foreach ($query->result() as $row) |
| 99 | { |
| 100 | echo $row->title; |
| 101 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 102 | |
| 103 | The above get() function retrieves all the results from the supplied |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 104 | table. The :doc:`Query Builder <query_builder>` class contains a full |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 105 | compliment of functions for working with data. |
| 106 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 107 | Query Builder Insert |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 108 | ==================== |
| 109 | |
| 110 | :: |
| 111 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 112 | $data = array( |
| 113 | 'title' => $title, |
| 114 | 'name' => $name, |
| 115 | 'date' => $date |
| 116 | ); |
| 117 | |
| 118 | $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] | 119 | |