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 | |
| 27 | $query = $this->db->query('SELECT name, title, email FROM my_table'); foreach ($query->result() as $row) { echo $row->title; echo $row->name; echo $row->email; } echo 'Total Results: ' . $query->num_rows(); |
| 28 | |
| 29 | The above result() function returns an array of **objects**. Example: |
| 30 | $row->title |
| 31 | |
| 32 | Standard Query With Multiple Results (Array Version) |
| 33 | ==================================================== |
| 34 | |
| 35 | :: |
| 36 | |
| 37 | $query = $this->db->query('SELECT name, title, email FROM my_table'); foreach ($query->result_array() as $row) { echo $row['title']; echo $row['name']; echo $row['email']; } |
| 38 | |
| 39 | The above result_array() function returns an array of standard array |
| 40 | indexes. Example: $row['title'] |
| 41 | |
| 42 | Testing for Results |
| 43 | =================== |
| 44 | |
| 45 | If you run queries that might **not** produce a result, you are |
| 46 | encouraged to test for a result first using the num_rows() function:: |
| 47 | |
| 48 | $query = $this->db->query("YOUR QUERY"); if ($query->num_rows() > 0) { foreach ($query->result() as $row) { echo $row->title; echo $row->name; echo $row->body; } } |
| 49 | |
| 50 | Standard Query With Single Result |
| 51 | ================================= |
| 52 | |
| 53 | :: |
| 54 | |
| 55 | $query = $this->db->query('SELECT name FROM my_table LIMIT 1'); $row = $query->row(); echo $row->name; |
| 56 | |
| 57 | The above row() function returns an **object**. Example: $row->name |
| 58 | |
| 59 | Standard Query With Single Result (Array version) |
| 60 | ================================================= |
| 61 | |
| 62 | :: |
| 63 | |
| 64 | $query = $this->db->query('SELECT name FROM my_table LIMIT 1'); $row = $query->row_array(); echo $row['name']; |
| 65 | |
| 66 | The above row_array() function returns an **array**. Example: |
| 67 | $row['name'] |
| 68 | |
| 69 | Standard Insert |
| 70 | =============== |
| 71 | |
| 72 | :: |
| 73 | |
| 74 | $sql = "INSERT INTO mytable (title, name) VALUES (".$this->db->escape($title).", ".$this->db->escape($name).")"; $this->db->query($sql); echo $this->db->affected_rows(); |
| 75 | |
| 76 | Active Record Query |
| 77 | =================== |
| 78 | |
| 79 | The :doc:`Active Record Pattern <active_record>` gives you a simplified |
| 80 | means of retrieving data:: |
| 81 | |
| 82 | $query = $this->db->get('table_name'); foreach ($query->result() as $row) { echo $row->title; } |
| 83 | |
| 84 | The above get() function retrieves all the results from the supplied |
| 85 | table. The :doc:`Active Record <active_record>` class contains a full |
| 86 | compliment of functions for working with data. |
| 87 | |
| 88 | Active Record Insert |
| 89 | ==================== |
| 90 | |
| 91 | :: |
| 92 | |
| 93 | $data = array( 'title' => $title, 'name' => $name, 'date' => $date ); $this->db->insert('mytable', $data); // Produces: INSERT INTO mytable (title, name, date) VALUES ('{$title}', '{$name}', '{$date}') |
| 94 | |