blob: a85b89befc8c997c71daf77aa9e987d1b5834eb6 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001########################
2Generating Query Results
3########################
4
5There are several ways to generate query results:
6
7result()
8========
9
10This function returns the query result as an array of **objects**, or
11**an empty array** on failure. Typically you'll use this in a foreach
12loop, like this::
13
14 $query = $this->db->query("YOUR QUERY"); foreach ($query->result() as $row) {    echo $row->title;    echo $row->name;    echo $row->body; }
15
16The above function is an alias of result_object().
17
18If you run queries that might **not** produce a result, you are
19encouraged to test the result first::
20
21 $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;    } }
22
23You can also pass a string to result() which represents a class to
24instantiate for each result object (note: this class must be loaded)
25
26::
27
28 $query = $this->db->query("SELECT * FROM users;");
29
30 foreach ($query->result('User') as $user)
31 {
32 echo $user->name; // call attributes
33 echo $user->reverse_name(); // or methods defined on the 'User' class
34 }
35
36result_array()
37===============
38
39This function returns the query result as a pure array, or an empty
40array when no result is produced. Typically you'll use this in a foreach
41loop, like this::
42
43 $query = $this->db->query("YOUR QUERY"); foreach ($query->result_array() as $row) {    echo $row['title'];    echo $row['name'];    echo $row['body']; }
44
45row()
46=====
47
48This function returns a single result row. If your query has more than
49one row, it returns only the first row. The result is returned as an
50**object**. Here's a usage example::
51
52 $query = $this->db->query("YOUR QUERY"); if ($query->num_rows() > 0) {    $row = $query->row();    echo $row->title;    echo $row->name;    echo $row->body; }
53
54If you want a specific row returned you can submit the row number as a
55digit in the first parameter::
56
57 $row = $query->row(5);
58
59You can also add a second String parameter, which is the name of a class
60to instantiate the row with::
61
62 $query = $this->db->query("SELECT * FROM users LIMIT 1;"); $query->row(0, 'User') echo $row->name; // call attributes echo $row->reverse_name(); // or methods defined on the 'User' class
63
64row_array()
65============
66
67Identical to the above row() function, except it returns an array.
68Example::
69
70 $query = $this->db->query("YOUR QUERY"); if ($query->num_rows() > 0) {    $row = $query->row_array();    echo $row['title'];    echo $row['name'];    echo $row['body']; }
71
72If you want a specific row returned you can submit the row number as a
73digit in the first parameter::
74
75 $row = $query->row_array(5);
76
77In addition, you can walk forward/backwards/first/last through your
78results using these variations:
79
80**$row = $query->first_row()**
81 **$row = $query->last_row()**
82 **$row = $query->next_row()**
83 **$row = $query->previous_row()**
84
85By default they return an object unless you put the word "array" in the
86parameter:
87
88**$row = $query->first_row('array')**
89 **$row = $query->last_row('array')**
90 **$row = $query->next_row('array')**
91 **$row = $query->previous_row('array')**
92
93***********************
94Result Helper Functions
95***********************
96
97$query->num_rows()
98===================
99
100The number of rows returned by the query. Note: In this example, $query
101is the variable that the query result object is assigned to::
102
103 $query = $this->db->query('SELECT * FROM my_table'); echo $query->num_rows();
104
105$query->num_fields()
106=====================
107
108The number of FIELDS (columns) returned by the query. Make sure to call
109the function using your query result object::
110
111 $query = $this->db->query('SELECT * FROM my_table'); echo $query->num_fields();
112
113$query->free_result()
114======================
115
116It frees the memory associated with the result and deletes the result
117resource ID. Normally PHP frees its memory automatically at the end of
118script execution. However, if you are running a lot of queries in a
119particular script you might want to free the result after each query
120result has been generated in order to cut down on memory consumptions.
121Example::
122
123 $query = $this->db->query('SELECT title FROM my_table'); foreach ($query->result() as $row) {    echo $row->title; } $query->free_result(); // The $query result object will no longer be available $query2 = $this->db->query('SELECT name FROM some_table'); $row = $query2->row(); echo $row->name; $query2->free_result(); // The $query2 result object will no longer be available
124