Tutorialsplane

Codeigniter Query Helper Methods


Codeigniter Query Helper Methods – This database reference provide many functions that are used to check insert, update, count all, version of data. It is also used to making your queries easier. Here in this tutorial, we are going to explain how to use Query Helper Methods.


Codeigniter Query Helper Methods | Example.

Let us understand how generating query helper methods works in codeigniter with examples.

Functions:-

There are following functions available in query helper methods . Now we will explain one by one.

1. Information From Executing a Query.

Here is simple demo of information from executing a query.

Example:-

$this->db->insert_id()

The insert id number when performing database insert.

$this->db->affected_rows()

Display the number of affected row.

$this->db->last_query()

Display the last query that was run.

Syntax of information from executing a query.

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class query_helper_controller extends CI_Controller 
{
public function execute_query()
{
echo $this-?>db->insert_id('blog');
echo "<br/>";
echo $this->db->affected_rows();
echo "<br/>";
echo $this->db->count_all('blog');
echo "<br/>";
echo $this->db->last_query();
}
}
?>

2. Information About Your Database.

Here is simple demo of information about your database.

Example:-

$this->db->count_all()

this query gives permit you to determine the number of rows in a particular table.

$this->db->platform()

Display the output the database platform you are running like(MYSQL, MS SQL, Postgres ) etc.

$this->db->version()

Display the database version you are running.

Syntax of information about your database.

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class query_helper_controller extends CI_Controller 
{
public function database_information()
{
echo $this-?>db->count_all('blog');
echo "<br/>";
echo $this->db->platform();
echo "<br/>";
echo $this->db->version();
}
}
?>

3. Making Your Queries Easier.

Here is simple demo of making your queries easier.

Example:-

$this->db->insert_string()

This function reduce the process of writing database insert and return correctly formatted SQL insert string.

the first parameter is the table name and second one is an associative array with the data to be inserted.

$this->db->update_string()

This function reduce the process of writing database update and return correctly formatted SQL update string.

Syntax of making your queries easier.

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class query_helper_controller extends CI_Controller 
{
public function database_information()
{
echo $this-?>db->count_all('blog');
echo "<br/>";
echo $this->db->platform();
echo "<br/>";
echo $this->db->version();
}
}
?>

Models

Connect Database

Helpers

Libraries

Helper Reference

Library Reference

Database Reference