From b0dd10f8171945e0c1f3527dd1e9d18b043e01a7 Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 25 Aug 2006 17:25:49 +0000 Subject: Initial Import --- user_guide/libraries/database/results.html | 235 +++++++++++++++++++++++++++++ 1 file changed, 235 insertions(+) create mode 100644 user_guide/libraries/database/results.html (limited to 'user_guide/libraries/database/results.html') diff --git a/user_guide/libraries/database/results.html b/user_guide/libraries/database/results.html new file mode 100644 index 000000000..7e6b95d2f --- /dev/null +++ b/user_guide/libraries/database/results.html @@ -0,0 +1,235 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

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

Query Results

+ + +

There are several ways to generate query results:

+ +

result()

+ +

This function returns the query result as an array of objects, or FALSE on failure. + + Typically you'll use this in a foreach loop, like this:

+ + + $query = $this->db->query("YOUR QUERY");
+
+ foreach ($query->result() as $row)
+ {
+    echo $row->title;
+    echo $row->name;
+    echo $row->body;
+ }
+ +

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

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

result_array()

+ +

This function returns the query result as a pure array, or FALSE on failure. Typically you'll use this in a foreach loop, like this:

+ + $query = $this->db->query("YOUR QUERY");
+
+ foreach ($query->result_array() as $row)
+ {
+    echo $row['title'];
+    echo $row['name'];
+    echo $row['body'];
+ }
+ + +

row()

+ +

This function returns a single result row. If your query has more than one row, it returns only the first row. + The result is returned as an object. Here's a usage example:

+ + $query = $this->db->query("YOUR QUERY");
+
+ if ($query->num_rows() > 0)
+ {
+    $row = $query->row(); +

+    echo $row->title;
+    echo $row->name;
+    echo $row->body;
+ } +
+ +

If you want a specific row returned you can submit the row number as a digit in the first parameter: + + $row = $query->row(5); + + +

row_array()

+ +

Identical to the above row() function, except it returns an array. Example:

+ + + $query = $this->db->query("YOUR QUERY");
+
+ if ($query->num_rows() > 0)
+ {
+    $row = $query->row_array(); +

+    echo $row['title'];
+    echo $row['name'];
+    echo $row['body'];
+ } +
+ + +

If you want a specific row returned you can submit the row number as a digit in the first parameter: + + $row = $query->row_array(5); + + +

In addition, you can walk forward/backwards/first/last through your results using these variations:

+ +

+ $row = $query->first_row()
+ $row = $query->last_row()
+ $row = $query->next_row()
+ $row = $query->previous_row() +

+ +

By default they return an object unless you put the word "array" in the parameter:

+ +

+ $row = $query->first_row('array')
+ $row = $query->last_row('array')
+ $row = $query->next_row('array')
+ $row = $query->previous_row('array') +

+ + +


Query Result Helpers

+ +

The following functions provide useful information when dealing with query results.

+ +

$query->num_rows()

+

The number of rows returned by the query. Note: $query is the variable that the query was assigned to:

+ +$query = $this->db->query('SELECT * FROM my_table');

+echo $query->num_rows(); +
+ +

$query->num_fields()

+

The number of FIELDS returned by the query. Make sure to call the function using your query result object:

+ +$query = $this->db->query('SELECT * FROM my_table');

+echo $query->num_fields(); +
+ + +

$this->db->insert_id()

+

The insert ID number when performing database inserts.

+ +

$this->db->affected_rows()

+

Displays the number of affected rows, when doing "write" type queries (insert, update, etc.).

+

Note: In MySQL "DELETE FROM TABLE" returns 0 affected rows. The database class has a small hack that allows it to return the +correct number of affected rows. By default this hack is enabled but it can be turned off in the database driver file.

+ + +

$this->db->version()

+

Outputs the database version you are running:

+ +echo $this->db->version(); + + + + + +
+ + + + + + + \ No newline at end of file -- cgit v1.2.3-24-g4f1b