1 <html xmlns=
"http://www.w3.org/1999/xhtml" xml:
lang=
"en">
3 <title>CarbonPHP Documentation
</title>
4 <style type=
"text/css">
11 font-family: "Lucida Sans Unicode", "Lucida Grande";
33 margin: 10px 0 10px 0;
34 list-style-position: inside
;
42 border: 1px dotted
#444;
50 border: 1px solid
#ccc;
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>
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>
74 $this-
>load-
>database();
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>
83 <h1>Simply Query With an Object Result
</h1>
86 $query = $this-
>db-
>query('SELECT * FROM `some_table`');
<br />
88 foreach ($query-
>result() as $row)
<br />
90 echo $row-
>title;
<br />
91 echo $row-
>author;
<br />
92 echo $row-
>body;
<br />
95 echo 'Total results: ' . $query-
>num_rows();
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>
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 />
108 foreach ($query-
>result_array() as $row)
<br />
110 echo $row['title'];
<br />
111 echo $row['author'];
<br />
112 echo $row['body'];
<br />
115 echo 'Total results: ' . $query-
>num_rows();
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 />
125 $row = $query-
>row();
129 <p>The above example uses the
<b>row()
</b> method to return an object.
</p>
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 />
138 $row = $query-
>row_array();
142 <p>The above example uses the
<b>row_array()
</b> method to return an array.
</p>
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 />
152 $this-
>db-
>query($sql);
<br />
154 echo $this-
>db-
>affected_rows();
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 />
166 foreach ($query-
>result() as $row)
<br />
168 echo $row-
>author;
<br />
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>
176 <div class=
"content">
177 <h1>Simple Active Record Insert Query
</h1>
179 <div class=
"example">
181 'title' =
> $title,
<br />
182 'author' =
> $author,
<br />
183 'post' =
> $post
<br />
186 $this-
>db-
>insert('some_table', $data);
<br />
188 // Will produce: INSERT INTO `some_table` (title, author, post) VALUES ('{title}', '{$author}', '{$post}')
191 <p>The
<b>insert()
</b> method inserts data into the specified table name, you can pass either an array or