blob: 5fd7fccfa094ad239f688df7156ed7163d1f3bc4 [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
Derek Jones8ede1a22011-10-05 13:34:52 -050058Standard Query With Single Result
59=================================
60
61::
62
Joseph Wensleyf24f4042011-10-06 22:53:29 -040063 $query = $this->db->query('SELECT name FROM my_table LIMIT 1');
64 $row = $query->row();
65 echo $row->name;
Derek Jones8ede1a22011-10-05 13:34:52 -050066
67The above row() function returns an **object**. Example: $row->name
68
69Standard Query With Single Result (Array version)
70=================================================
71
72::
73
Joseph Wensleyf24f4042011-10-06 22:53:29 -040074 $query = $this->db->query('SELECT name FROM my_table LIMIT 1');
75 $row = $query->row_array();
76 echo $row['name'];
Derek Jones8ede1a22011-10-05 13:34:52 -050077
78The above row_array() function returns an **array**. Example:
79$row['name']
80
81Standard Insert
82===============
83
84::
85
Joseph Wensleyf24f4042011-10-06 22:53:29 -040086 $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 Jones8ede1a22011-10-05 13:34:52 -050089
Jamie Rumbelow7efad202012-02-19 12:37:00 +000090Query Builder Query
Derek Jones8ede1a22011-10-05 13:34:52 -050091===================
92
Jamie Rumbelow7efad202012-02-19 12:37:00 +000093The :doc:`Query Builder Pattern <query_builder>` gives you a simplified
Derek Jones8ede1a22011-10-05 13:34:52 -050094means of retrieving data::
95
Joseph Wensleyf24f4042011-10-06 22:53:29 -040096 $query = $this->db->get('table_name');
97
98 foreach ($query->result() as $row)
99 {
100 echo $row->title;
101 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500102
103The above get() function retrieves all the results from the supplied
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000104table. The :doc:`Query Builder <query_builder>` class contains a full
Derek Jones8ede1a22011-10-05 13:34:52 -0500105compliment of functions for working with data.
106
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000107Query Builder Insert
Derek Jones8ede1a22011-10-05 13:34:52 -0500108====================
109
110::
111
Joseph Wensleyf24f4042011-10-06 22:53:29 -0400112 $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 Jones8ede1a22011-10-05 13:34:52 -0500119