blob: d1cd4883724e77ca65d51738dceb4f80cbc3aa65 [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
Joseph Wensleyf24f4042011-10-06 22:53:29 -040027 $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 Jones8ede1a22011-10-05 13:34:52 -050037
38The above result() function returns an array of **objects**. Example:
39$row->title
40
41Standard Query With Multiple Results (Array Version)
42====================================================
43
44::
45
Joseph Wensleyf24f4042011-10-06 22:53:29 -040046 $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 Jones8ede1a22011-10-05 13:34:52 -050054
55The above result_array() function returns an array of standard array
56indexes. Example: $row['title']
57
58Testing for Results
59===================
60
61If you run queries that might **not** produce a result, you are
62encouraged to test for a result first using the num_rows() function::
63
Joseph Wensleyf24f4042011-10-06 22:53:29 -040064 $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 Jones8ede1a22011-10-05 13:34:52 -050074
75Standard Query With Single Result
76=================================
77
78::
79
Joseph Wensleyf24f4042011-10-06 22:53:29 -040080 $query = $this->db->query('SELECT name FROM my_table LIMIT 1');
81 $row = $query->row();
82 echo $row->name;
Derek Jones8ede1a22011-10-05 13:34:52 -050083
84The above row() function returns an **object**. Example: $row->name
85
86Standard Query With Single Result (Array version)
87=================================================
88
89::
90
Joseph Wensleyf24f4042011-10-06 22:53:29 -040091 $query = $this->db->query('SELECT name FROM my_table LIMIT 1');
92 $row = $query->row_array();
93 echo $row['name'];
Derek Jones8ede1a22011-10-05 13:34:52 -050094
95The above row_array() function returns an **array**. Example:
96$row['name']
97
98Standard Insert
99===============
100
101::
102
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400103 $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 Jones8ede1a22011-10-05 13:34:52 -0500106
107Active Record Query
108===================
109
110The :doc:`Active Record Pattern <active_record>` gives you a simplified
111means of retrieving data::
112
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400113 $query = $this->db->get('table_name');
114
115 foreach ($query->result() as $row)
116 {
117 echo $row->title;
118 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500119
120The above get() function retrieves all the results from the supplied
121table. The :doc:`Active Record <active_record>` class contains a full
122compliment of functions for working with data.
123
124Active Record Insert
125====================
126
127::
128
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400129 $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 Jones8ede1a22011-10-05 13:34:52 -0500136