Codeigniter Generating Query Results
Codeigniter Generating Query Results – This database reference is used for generate the query results. Here in this tutorial, we are going to explain how to use generating query results.
Codeigniter Generating Query Results | Example.
Let us understand how generating query result works in codeigniter with examples.
Functions:-
There are following way to generate query results. Now we will explain one by one.
- 1. Result Arrays.
- 2. Result Rows.
- 3. Custom Result objects.
- 4. Result Helper Methods.
1. Result Arrays.
Here is simple demo of result arrays.
Example:-
Syntax of result arrays.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class query_result_controller extends CI_Controller { public function ResultArray() { $query = $this->db->query("select * from blog"); foreach ($query->result() as $row) { echo $row->title; echo $row->name; echo "<br>"; } } } ?> |
Output will be like this:-
2. Result Rows.
Here is simple demo of result rows.
Example:-
Syntax of result rows.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class query_result_controller extends CI_Controller { public function ResultRows() { $query = $this->db->query("select * from blog"); $row = $query->row(); if (isset($row)) { echo $row->title; echo $row->name; echo "<br>"; } } } ?> |
Output will be like this:-
3. Custom Result objects.
Here is simple demo of custom result objects.
Example:-
Syntax of custom result objects.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class query_result_controller extends CI_Controller { } ?> |
Output will be like this:-
4. Result Helper Methods.
Here is simple demo of result helper methods.
Example:-
Syntax of result helper methods.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class query_result_controller extends CI_Controller { public function helper_method() { //num rows $query = $this->db->query('SELECT * FROM blog'); echo $query->num_rows(); echo "<br>"; //num field echo $query->num_fields(); echo "<br>"; //free result foreach ($query->result() as $row) { echo $row->title; } $query2 = $this->db->query('SELECT name FROM blog'); $query->free_result(); echo "<br>"; $row = $query2->row(); echo $row->name; $query2->free_result(); } } ?> |
Output will be like this:-
Class reference:-
There are following references available in generating query results. Now we will explain.
1. Result.
This reference is used to wrapping for the result_array() method.
result([$type = 'object'])
- Parameters :
- $type (string) : Type of requested results – array, object, or class name.
- Returns : Array containing the fetched rows.
- Returns type : Array.
2. Result array.
This reference is used to return the query result as an array of row.
result_array()
- Parameters :
- Returns : Array containing the fetched rows.
- Returns type : Array.
3. Result Object.
This reference is used to return the query result as an array of row. Where each row is an object of type.
result_object()
- Parameters :
- Returns : Array containing the fetched rows.
- Returns type : Array.
4. Custom result object.
Here each row is instance of class.
custom_result_object($class_name)
- Parameters :
- $class_name (string) : Class name for the resulting rows.
- Returns : Array containing the fetched rows.
- Returns type : Array.
5. Row.
A wrapper of row_array() method.
row([$n = 0[$type = 'object']])
- Parameters :
- $n (int) : Index of the query results row to be returned.
- $type (string) : Type of the requested result – array, object, or class name.
- Returns : The requested row or NULL if it doesn’t exist.
- Returns type : mixed.
6. Unbuffered row.
This refernce is used to fetch the next row and return in the requested form.
unbuffered_row([$type = 'object'])
- Parameters :
- $type (string) : Type of the requested result – array, object, or class name.
- Returns : Next row from the result set or NULL if it doesn’t exist.
- Returns type : mixed.
7. Row array.
This refernce is used to returns the requested result row as an associative array.
row_array([$n = 0])
- Parameters :
- $n (int) : Index of the query results row to be returned.
- Returns : The requested row or NULL if it doesn’t exist.
- Returns type : Array.
8. Row object.
This refernce is used to returns the requested result row as an object of type stdclass.
row_object([$n = 0])
- Parameters :
- $n (int) : Index of the query results row to be returned.
- Returns : The requested row or NULL if it doesn’t exist.
- Returns type : stdClass.
9. Custom row object.
This refernce is used to returns instance of the requested class.
custom_row_object($n, $type)
- Parameters :
- $n (int) : Index of the results row to return.
- $class_name (string) : Class name for the resulting row.
- Returns : The requested row or NULL if it doesn’t exist.
- Returns type : $type.
10. Data seek.
This refernce is used to move to internal result row pointer to the desired offset.
data_seek([$n = 0])
- Parameters :
- $n (int) : Index of the results row to be returned next.
- Returns : TRUE on success, FALSE on failure.
- Returns type : Bool.
11. Set row.
This refernce is used to assisgn a value to a particular column.
set_row($key[$value = NULL])
- Parameters :
- $key (mixed) : Column name or array of key/value pairs.
- $value (mixed) : Value to assign to the column, $key is a single field name.
- Returns type : void.
12. Next row.
This refernce is used to assisgn a value to a particular column.
next_row([$type = 'object'])
- Parameters :
- $type (string) : Type of the requested result – array, object, or class name.
- Returns : Next row of result set, or NULL if it doesn’t exist.
- Returns type : Mixed.
13. Previous row.
This refernce return the previous row from the result set.
previous_row([$type = 'object'])
- Parameters :
- $type (string) : Type of the requested result – array, object, or class name.
- Returns : Previous row of result set, or NULL if it doesn’t exist.
- Returns type : Mixed.
14. First row.
This refernce return the first row from the result set.
previous_row([$type = 'object'])
- Parameters :
- $type (string) : Type of the requested result – array, object, or class name.
- Returns : First row of result set, or NULL if it doesn’t exist.
- Returns type : Mixed.
15. Last row.
This refernce return the last row from the result set.
last_row([$type = 'object'])
- Parameters :
- $type (string) : Type of the requested result – array, object, or class name.
- Returns : Last row of result set, or NULL if it doesn’t exist.
- Returns type : Mixed.
16. Num rows.
This refernce return the number of row from the result set.
num_rows()
- Parameters :
- Returns : Number of row in the result set.
- Returns type : Mixed.
17. Num field.
This refernce return the number of field from the result set.
num_fields()
- Parameters :
- Returns : Number of fields in the result set.
- Returns type : Mixed.
18. Field data.
This refernce is used to generate an array of stdclass object containing field meta-data.
field_data()
- Parameters :
- Returns : Array containing field meta-data.
- Returns type : Array .
19. Free result.
This refernce provide free a result set.
free_result()
- Parameters :
- Returns type : void.
20. List fields.
This refernce return an array containing the field name in the result set.
list_fields()
- Parameters :
- Returns : Array of column names.
- Returns type : void.
Advertisements