Tutorialsplane

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.

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'])

2. Result array.

This reference is used to return the query result as an array of row.

result_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()

4. Custom result object.

Here each row is instance of class.

custom_result_object($class_name)

5. Row.

A wrapper of row_array() method.

row([$n = 0[$type = 'object']])

6. Unbuffered row.

This refernce is used to fetch the next row and return in the requested form.

unbuffered_row([$type = 'object'])

7. Row array.

This refernce is used to returns the requested result row as an associative array.

row_array([$n = 0])

8. Row object.

This refernce is used to returns the requested result row as an object of type stdclass.

row_object([$n = 0])

9. Custom row object.

This refernce is used to returns instance of the requested class.

custom_row_object($n, $type)

10. Data seek.

This refernce is used to move to internal result row pointer to the desired offset.

data_seek([$n = 0])

11. Set row.

This refernce is used to assisgn a value to a particular column.

set_row($key[$value = NULL])

12. Next row.

This refernce is used to assisgn a value to a particular column.

next_row([$type = 'object'])

13. Previous row.

This refernce return the previous row from the result set.

previous_row([$type = 'object'])

14. First row.

This refernce return the first row from the result set.

previous_row([$type = 'object'])

15. Last row.

This refernce return the last row from the result set.

last_row([$type = 'object'])

16. Num rows.

This refernce return the number of row from the result set.

num_rows()

17. Num field.

This refernce return the number of field from the result set.

num_fields()

18. Field data.

This refernce is used to generate an array of stdclass object containing field meta-data.

field_data()

19. Free result.

This refernce provide free a result set.

free_result()

20. List fields.

This refernce return an array containing the field name in the result set.

list_fields()

Models

Connect Database

Helpers

Libraries

Helper Reference

Library Reference

Database Reference