In this tutorial you will learn about the CodeIgniter Result Functions and its application with practical example.
CodeIgniter Result Functions
In CodeIgniter, when you run active record query it returns the codeigniter resultset instance. In order, to fetch the values from the resultset instance you must have to use one of the result functions.
You can fetch the result-set in following ways –
result()
The result() function returns the result as an array of objects, or an empty array on failure.
Example:-
1 2 3 4 5 6 7 8 9 10 11 |
$query = $this->db->query("SELECT * FROM employee_master"); if ($query->num_rows() > 0) { foreach ($query->result() as $row) { echo $row->emp_name; echo $row->emp_email; echo $row->emp_phone; } } |
result_array()
The result_array() function returns the result as an array, or an empty array when no record found.
Example:-
1 2 3 4 5 6 7 8 9 10 11 |
$query = $this->db->query("SELECT * FROM employee_master"); if ($query->num_rows() > 0) { foreach ($query->result_array() as $row) { echo $row['emp_name']; echo $row['emp_email']; echo $row['emp_phone']; } } |
row()
The row() function returns a single result row. If your query returns more than one records, then the row() function returns only the first row. The result is returned as an object.
Example:-
1 2 3 4 5 6 7 8 9 |
$query = $this->db->query("SELECT * FROM employee_master"); if ($query->num_rows() > 0) { $row = $query->row(); echo $row->emp_name; echo $row->emp_email; echo $row->emp_phone; } |
If your query returns more than one records, you can access specific record by passing the row number.
Example:-
1 2 3 4 5 6 7 8 9 |
$query = $this->db->query("SELECT * FROM employee_master"); if ($query->num_rows() > 0) { $row = $query->row(10); echo $row->emp_name; echo $row->emp_email; echo $row->emp_phone; } |
row_array()
The row_array() function is similar to the row() function, except that it returns result row as an array.
Example:-
1 2 3 4 5 6 7 8 9 |
$query = $this->db->query("SELECT * FROM employee_master"); if ($query->num_rows() > 0) { $row = $query->row(10); echo $row['emp_name']; echo $row['emp_email']; echo $row['emp_phone']; } |
You can traverse through the results forward/backwards/first/last using following 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->num_rows()
The num_rows() function is used to determine the number of rows in query result-set.
Example:-
1 2 3 |
$query = $this->db->query('SELECT * FROM employee_master'); echo $query->num_rows(); |
$query->num_fields()
The num_rows() function is used to determine the number of FIELDS (columns) in query result-set instance.
Example:-
1 2 3 |
$query = $this->db->query('SELECT * FROM employee_master'); echo $query->num_fields(); |
$query->free_result()
The free_result() function frees the memory associated with the query result-set and deletes the result resource ID.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 |
$query = $this->db->query("SELECT * FROM employee_master"); if ($query->num_rows() > 0) { foreach ($query->result() as $row) { echo $row->emp_name; echo $row->emp_email; echo $row->emp_phone; } } $query->free_result(); // The $query result object will no longer be available |