summaryrefslogtreecommitdiffstats
path: root/user_guide_src/source/database/examples.rst
diff options
context:
space:
mode:
authorGreg Aker <greg@gregaker.net>2011-10-07 05:22:48 +0200
committerGreg Aker <greg@gregaker.net>2011-10-07 05:22:48 +0200
commit02db0666a8b4da6a58db0e2f6755143ce1b21971 (patch)
tree5f001704bb6a119693e40675a4951db6d462988e /user_guide_src/source/database/examples.rst
parent4c5dfdfb57fdbda43c79333fa5b3442f1f6c0629 (diff)
parentf24f404a34081241c1398f568b506e2c9d9bec5b (diff)
Merge branch 'user_guide' of https://github.com/jwensley2/CodeIgniter into jwensley2-user_guide
Conflicts: user_guide_src/source/database/configuration.rst
Diffstat (limited to 'user_guide_src/source/database/examples.rst')
-rw-r--r--user_guide_src/source/database/examples.rst58
1 files changed, 50 insertions, 8 deletions
diff --git a/user_guide_src/source/database/examples.rst b/user_guide_src/source/database/examples.rst
index bd2cc4d96..d1cd48837 100644
--- a/user_guide_src/source/database/examples.rst
+++ b/user_guide_src/source/database/examples.rst
@@ -24,7 +24,16 @@ 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();
+ $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
@@ -34,7 +43,14 @@ 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']; }
+ $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']
@@ -45,14 +61,25 @@ 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;    } }
+ $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;
+ $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
@@ -61,7 +88,9 @@ 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'];
+ $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']
@@ -71,7 +100,9 @@ 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();
+ $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
===================
@@ -79,7 +110,12 @@ Active Record Query
The :doc:`Active Record Pattern <active_record>` gives you a simplified
means of retrieving data::
- $query = $this->db->get('table_name'); foreach ($query->result() as $row) {     echo $row->title; }
+ $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 :doc:`Active Record <active_record>` class contains a full
@@ -90,5 +126,11 @@ 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}')
+ $data = array(
+ 'title' => $title,
+ 'name' => $name,
+ 'date' => $date
+ );
+
+ $this->db->insert('mytable', $data); // Produces: INSERT INTO mytable (title, name, date) VALUES ('{$title}', '{$name}', '{$date}')