CodeIgniter Result Functions

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:-

result_array()

The result_array() function returns the result as an array, or an empty array when no record found.

Example:-

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:-

If your query returns more than one records, you can access specific record by passing the row number.

Example:-

row_array()

The row_array() function is similar to the row() function, except that it returns result row as an array.

Example:-

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:-

$query->num_fields()

The num_rows() function is used to determine the number of FIELDS (columns) in query result-set instance.

Example:-

 

$query->free_result()

The free_result() function frees the memory associated with the query result-set and deletes the result resource ID.

Example:-

 

In this tutorial we have learn about the CodeIgniter Result Functions and its application with practical example. I hope you will like this tutorial.