From 2067d1a727e7eb5e5ffb40e967f3d1fc4c8a41b2 Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Thu, 13 Nov 2008 22:59:24 +0000 Subject: Changing EOL style to LF --- user_guide/database/examples.html | 432 +++++++++++++++++++------------------- 1 file changed, 216 insertions(+), 216 deletions(-) (limited to 'user_guide/database/examples.html') diff --git a/user_guide/database/examples.html b/user_guide/database/examples.html index 4182390f8..49d272992 100644 --- a/user_guide/database/examples.html +++ b/user_guide/database/examples.html @@ -1,217 +1,217 @@ - - - - - -Database Quick Start : CodeIgniter User Guide - - - - - - - - - - - - - - - - - - - - - -
- - - - - -

CodeIgniter User Guide Version 1.7

-
- - - - - - - - - - -
- - - -
- - - -
- - -

Database Quick Start: Example Code

- -

The following page contains example code showing how the database class is used. For complete details please -read the individual pages describing each function.

- - -

Initializing the Database Class

- -

The following code loads and initializes the database class based on your configuration settings:

- -$this->load->database(); - -

Once loaded the class is ready to be used as described below.

- -

Note: If all your pages require database access you can connect automatically. See the connecting page for details.

- - -

Standard Query With Multiple Results (Object Version)

- -$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(); -
- -

The above result() function returns an array of objects. Example: $row->title

- - -

Standard Query With Multiple Results (Array Version)

- -$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'];
-}
- -

The above result_array() function returns an array of standard array indexes. Example: $row['title']

- - -

Testing for Results

- -

If you run queries that might not produce a result, you are encouraged to test for a result first -using the num_rows() function:

- - -$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;
-   }
-} -
- - - - -

Standard Query With Single Result

- -$query = $this->db->query('SELECT name FROM my_table LIMIT 1');
-
-$row = $query->row();
-echo $row->name;
-
- -

The above row() function returns an object. Example: $row->name

- - -

Standard Query With Single Result (Array version)

- -$query = $this->db->query('SELECT name FROM my_table LIMIT 1');
-
-$row = $query->row_array();
-echo $row['name'];
-
- -

The above row_array() function returns an array. Example: $row['name']

- - -

Standard Insert

- - -$sql = "INSERT INTO mytable (title, name)
-        VALUES (".$this->db->escape($title).", ".$this->db->escape($name).")";
-
-$this->db->query($sql);
-
-echo $this->db->affected_rows(); -
- - - - -

Active Record Query

- -

The Active Record Pattern gives you a simplified means of retrieving data:

- - -$query = $this->db->get('table_name');
-
-foreach ($query->result() as $row)
-{
-    echo $row->title;
-}
- -

The above get() function retrieves all the results from the supplied table. -The Active Record class contains a full compliment of functions -for working with data.

- - -

Active Record Insert

- - -$data = array(
-               'title' => $title,
-               'name' => $name,
-               'date' => $date
-            );
-
-$this->db->insert('mytable', $data); -

-// Produces: INSERT INTO mytable (title, name, date) VALUES ('{$title}', '{$name}', '{$date}')
- - - - -
- - - - - - + + + + + +Database Quick Start : CodeIgniter User Guide + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

CodeIgniter User Guide Version 1.7

+
+ + + + + + + + + + +
+ + + +
+ + + +
+ + +

Database Quick Start: Example Code

+ +

The following page contains example code showing how the database class is used. For complete details please +read the individual pages describing each function.

+ + +

Initializing the Database Class

+ +

The following code loads and initializes the database class based on your configuration settings:

+ +$this->load->database(); + +

Once loaded the class is ready to be used as described below.

+ +

Note: If all your pages require database access you can connect automatically. See the connecting page for details.

+ + +

Standard Query With Multiple Results (Object Version)

+ +$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(); +
+ +

The above result() function returns an array of objects. Example: $row->title

+ + +

Standard Query With Multiple Results (Array Version)

+ +$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'];
+}
+ +

The above result_array() function returns an array of standard array indexes. Example: $row['title']

+ + +

Testing for Results

+ +

If you run queries that might not produce a result, you are encouraged to test for a result first +using the num_rows() function:

+ + +$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;
+   }
+} +
+ + + + +

Standard Query With Single Result

+ +$query = $this->db->query('SELECT name FROM my_table LIMIT 1');
+
+$row = $query->row();
+echo $row->name;
+
+ +

The above row() function returns an object. Example: $row->name

+ + +

Standard Query With Single Result (Array version)

+ +$query = $this->db->query('SELECT name FROM my_table LIMIT 1');
+
+$row = $query->row_array();
+echo $row['name'];
+
+ +

The above row_array() function returns an array. Example: $row['name']

+ + +

Standard Insert

+ + +$sql = "INSERT INTO mytable (title, name)
+        VALUES (".$this->db->escape($title).", ".$this->db->escape($name).")";
+
+$this->db->query($sql);
+
+echo $this->db->affected_rows(); +
+ + + + +

Active Record Query

+ +

The Active Record Pattern gives you a simplified means of retrieving data:

+ + +$query = $this->db->get('table_name');
+
+foreach ($query->result() as $row)
+{
+    echo $row->title;
+}
+ +

The above get() function retrieves all the results from the supplied table. +The Active Record class contains a full compliment of functions +for working with data.

+ + +

Active Record Insert

+ + +$data = array(
+               'title' => $title,
+               'name' => $name,
+               'date' => $date
+            );
+
+$this->db->insert('mytable', $data); +

+// Produces: INSERT INTO mytable (title, name, date) VALUES ('{$title}', '{$name}', '{$date}')
+ + + + +
+ + + + + + \ No newline at end of file -- cgit v1.2.3-24-g4f1b