CodeIgniter Query Helper

CodeIgniter's comes with following Query Helper Functions.

$this->db->insert_id()

Return insert ID number while we performing database inserts.

$this->db->affected_rows()

Return number of affected rows.when we performing insert, update, etc.

Note:MySQL DELETE operation returns 0 affected rows.

$this->db->count_all()

Return number of rows in a particular table.Pass table name as parameter.

echo $this->db->count_all('table_name'); //Output:10

$this->db->platform()

Return platform you are running (MySQL, MS SQL, Postgres, etc...)

echo $this->db->platform(); //Output:MySQL

$this->db->version()

Return the database version you are running.

$this->db->last_query();

Return the last query (the query string, not the result).

$sql = $this->db->last_query(); // Output: SELECT * FROM table_name

$this->db->insert_string()

Return formatted SQL insert string.

$data = array('name' => $name, 'email' => $email); $sql = $this->db->insert_string('table_name', $data); // Output: INSERT INTO table_name (name, email) VALUES ('Roje', '[email protected]')

$this->db->update_string()

Return formatted SQL update string.

$data = array('name' => $name, 'email' => $email); $where = "id = 1"; $sql= $this->db->update_string('table_name', $data, $where); // Output:UPDATE table_name SET name = 'Rose', email = '[email protected]' WHERE id = 1