blob: bd2cc4d96a9d412b122429cf7cc70b86efd2a69e [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001##################################
2Database Quick Start: Example Code
3##################################
4
5The following page contains example code showing how the database class
6is used. For complete details please read the individual pages
7describing each function.
8
9Initializing the Database Class
10===============================
11
12The following code loads and initializes the database class based on
13your :doc:`configuration <configuration>` settings::
14
15 $this->load->database();
16
17Once loaded the class is ready to be used as described below.
18
19Note: If all your pages require database access you can connect
20automatically. See the :doc:`connecting <connecting>` page for details.
21
22Standard 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
29The above result() function returns an array of **objects**. Example:
30$row->title
31
32Standard 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
39The above result_array() function returns an array of standard array
40indexes. Example: $row['title']
41
42Testing for Results
43===================
44
45If you run queries that might **not** produce a result, you are
46encouraged 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
50Standard 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
57The above row() function returns an **object**. Example: $row->name
58
59Standard 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
66The above row_array() function returns an **array**. Example:
67$row['name']
68
69Standard 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
76Active Record Query
77===================
78
79The :doc:`Active Record Pattern <active_record>` gives you a simplified
80means of retrieving data::
81
82 $query = $this->db->get('table_name'); foreach ($query->result() as $row) {     echo $row->title; }
83
84The above get() function retrieves all the results from the supplied
85table. The :doc:`Active Record <active_record>` class contains a full
86compliment of functions for working with data.
87
88Active 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