codeigniter 4 last query code example

Example 1: codeigniter get last query

$this->db->last_query();

Returns the last query that was run (the query string, not the result). Example:
$str = $this->db->last_query();

// Produces: SELECT * FROM sometable....

Example 2: print last query in codeigniter

We can get last executed query using last_query() function of db class in codeigniter. It is a very simple to use $this->db->last_query() function to see SQL statements of last executed query in php codeigniter app. You have to simple code that function after your main query that you wanted check 
$this->db->last_query()

Example 3: codeigniter echo last query

public function test_db(){    $query = $this->db->get("items");      $str = $this->db->last_query();       echo "<pre>";    print_r($str);    exit;}

Example 4: codeigniter 4 query builder get inserted id

$query = $this->db->table($this->table)->insert($data);
return $query->resultID;

Tags:

Misc Example