From b2a9ceccdb85050cb494e6d0a98b0a49495d29bb Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 1 Oct 2006 03:38:04 +0000 Subject: --- user_guide/database/examples.html | 41 ++++++++++++++++++++++++++++++++++++++- user_guide/database/fields.html | 17 ++++++++++++++++ user_guide/database/results.html | 6 ++---- 3 files changed, 59 insertions(+), 5 deletions(-) (limited to 'user_guide/database') diff --git a/user_guide/database/examples.html b/user_guide/database/examples.html index c58e9bb64..08051cadd 100644 --- a/user_guide/database/examples.html +++ b/user_guide/database/examples.html @@ -112,6 +112,28 @@ foreach ($query->result_array() as $row)

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');
@@ -120,6 +142,19 @@ $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

@@ -146,7 +181,11 @@ 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

diff --git a/user_guide/database/fields.html b/user_guide/database/fields.html index 260cd222f..83abe6327 100644 --- a/user_guide/database/fields.html +++ b/user_guide/database/fields.html @@ -94,6 +94,23 @@ foreach ($query->list_fields() as $field)
+

$this->db->field_exists();

+ +

Sometimes it's helpful to know whether a particular field exists befor performing an action. +Returns a boolean TRUE/FALSE. Usage example:

+ + +if ($this->db->field_exists('field_name', 'table_name'))
+{
+   // some code...
+} +
+ +

Note: Replace field_name with the name of the column you are looking for, and replace +table_name with the name of the table you are looking for.

+ + +

$this->db->field_data();

Returns an array of objects containing field information.

diff --git a/user_guide/database/results.html b/user_guide/database/results.html index 81abc50f8..75f4357b4 100644 --- a/user_guide/database/results.html +++ b/user_guide/database/results.html @@ -84,6 +84,8 @@ Query Results    echo $row->name;
   echo $row->body;
} + +

The above function is an alias of result_object().

If you run queries that might not produce a result, you are encouraged to test the result first:

@@ -113,10 +115,6 @@ Query Results    echo $row['name'];
   echo $row['body'];
} - -

result('array')

- -

Identical to $this->db->result_array().

row()

-- cgit v1.2.3-24-g4f1b