carbonPHP Initial commit v2.0
[carbonphp.git] / Documentation / database / quick.html
blob9b91aa963f849845a5bf2ed3c96089c7dcb482e5
1 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
2 <head>
3 <title>CarbonPHP Documentation</title>
4 <style type="text/css">
5 * {
6 margin: 0;
7 padding: 0;
10 html {
11 font-family: "Lucida Sans Unicode", "Lucida Grande";
12 font-size: 12px;
15 body {
16 background: #fff;
17 color: #666;
20 h1 {
21 font-size: 15px;
22 font-weight: bold;
23 margin: 0 0 5px 0;
26 h2 {
27 font-size: 13px;
28 font-weight: bold;
29 margin: 5px 0 5px 0;
32 ol, ul {
33 margin: 10px 0 10px 0;
34 list-style-position: inside;
37 p {
38 margin: 5px 0 5px 0;
41 .content {
42 border: 1px dotted #444;
43 margin: 10px;
44 padding: 10px;
45 text-align: justify;
46 width: 700px;
49 .example {
50 border: 1px solid #ccc;
51 background: #eee;
52 margin: 10px;
53 padding: 10px;
54 text-align: left;
56 </style>
57 </head>
59 <body>
60 <div class="content">
61 <h1>Database Quick Start</h1>
63 <p>This page will show you some quick examples showing how you can go about interacting with your database
64 using CarbonPHP. If you wish for a more in depth outline please read the individual pages.</p>
65 </div>
67 <div class="content">
68 <h1>Initialising and Connecting to a Database</h1>
70 <p>The following loads and initialises the database class which connects to a database based on your
71 configuration settings.</p>
73 <div class="example">
74 $this->load->database();
75 </div>
77 <p>Once the class has been loaded, the database object will be available via the <b>$this</b> object and
78 used as shown in the examples below. <i>Note</i>: if all your controllers/page require database access you
79 can autoload the database library.</p>
80 </div>
82 <div class="content">
83 <h1>Simply Query With an Object Result</h1>
85 <div class="example">
86 $query = $this->db->query('SELECT * FROM `some_table`');<br />
87 <br />
88 foreach ($query->result() as $row)<br />
89 &#123;<br />
90 &nbsp;&nbsp;&nbsp;&nbsp;echo $row->title;<br />
91 &nbsp;&nbsp;&nbsp;&nbsp;echo $row->author;<br />
92 &nbsp;&nbsp;&nbsp;&nbsp;echo $row->body;<br />
93 &#125;<br />
94 <br />
95 echo 'Total results: ' . $query->num_rows();
96 </div>
98 <p>The above example uses the <b>result()</b> method to return an array of objects. These objects will have
99 member variables corrosponding to the field names. Example <b>$row->title</b>.</p>
100 </div>
102 <div class="content">
103 <h1>Simple Query With an Array Result</h1>
105 <div class="example">
106 $query = $this->db->query('SELECT * FROM `some_table`');<br />
107 <br />
108 foreach ($query->result_array() as $row)<br />
109 &#123;<br />
110 &nbsp;&nbsp;&nbsp;&nbsp;echo $row['title'];<br />
111 &nbsp;&nbsp;&nbsp;&nbsp;echo $row['author'];<br />
112 &nbsp;&nbsp;&nbsp;&nbsp;echo $row['body'];<br />
113 &#125;<br />
114 <br />
115 echo 'Total results: ' . $query->num_rows();
116 </div>
117 </div>
119 <div class="content">
120 <h1>Simple Query With a Single Object Result</h1>
122 <div class="example">
123 $query = $this->db->query('SELECT title FROM `some_table` LIMIT 1');<br />
124 <br />
125 $row = $query->row();
126 echo $row->title;
127 </div>
129 <p>The above example uses the <b>row()</b> method to return an object.</p>
130 </div>
132 <div class="content">
133 <h1>Simple Query With a Single Array Result</h1>
135 <div class="example">
136 $query = $this->db->query('SELECT title FROM `some_table` LIMIT 1');<br />
137 <br />
138 $row = $query->row_array();
139 echo $row['title'];
140 </div>
142 <p>The above example uses the <b>row_array()</b> method to return an array.</p>
143 </div>
145 <div class="content">
146 <h1>Simple Insert Query</h1>
148 <div class="example">
149 $sql = 'INSERT INTO `some_table` (title, author) VALUES (' . $this->db->escape($title) . ', ' .
150 $this->db->escape($author) . ')';<br />
151 <br />
152 $this->db->query($sql);<br />
153 <br />
154 echo $this->db->affected_rows();
155 </div>
156 </div>
158 <div class="content">
159 <h1>Simple Active Record Query</h1>
161 <p>The <b>Active Record</b> design pattern gives you a simple interface for retrieving database data.</p>
163 <div class="example">
164 $query = $this->db->get('some_table');<br />
165 <br />
166 foreach ($query->result() as $row)<br />
167 &#123;<br />
168 &nbsp;&nbsp;&nbsp;&nbsp;echo $row->author;<br />
169 &#125;<br />
170 </div>
172 <p>The <b>get()</b> method retrieves all the results from the table name specified. The active record class
173 provides a range of methods for working with database data.</p>
174 </div>
176 <div class="content">
177 <h1>Simple Active Record Insert Query</h1>
179 <div class="example">
180 $data = array(<br />
181 &nbsp;&nbsp;&nbsp;&nbsp;'title' => $title,<br />
182 &nbsp;&nbsp;&nbsp;&nbsp;'author' => $author,<br />
183 &nbsp;&nbsp;&nbsp;&nbsp;'post' => $post<br />
184 );<br />
185 <br />
186 $this->db->insert('some_table', $data);<br />
187 <br />
188 // Will produce: INSERT INTO `some_table` (title, author, post) VALUES ('{title}', '{$author}', '{$post}')
189 </div>
191 <p>The <b>insert()</b> method inserts data into the specified table name, you can pass either an array or
192 object.</p>
193 </div>
194 </body>
196 </html>