Get last executed query in codeigniter
Get last executed query in codeigniter
With this article, we’ll look at some examples of how to address the "Get last executed query in codeigniter" problem.
You can get the last executed query in Codeigniter by simply using the last_query() function of the db class in Codeigniter 3, and the getLastQuery() method on the database connection class in Codeigniter 4.-
Get last executed query in codeigniter 3
--PATH app\Controllers\<Home>.phppublic function index() { $data['data'] = $this->db->get("users")->result(); $executedQuery = $this->db->last_query(); print_r($executedQuery); exit; }
SELECT * FROM `users`
You can get the last executed query in codeigniter 3 by calling the last_query() method on db class. You have to use the get() method on the users table to execute a query which we want to get and after that call last_query() method on db class to get the last executed query in Codeigniter 3.
-
Get last executed query in codeigniter 4
public function index() { $db = \Config\Database::connect(); $heroesCount = $db->table('heroes')->countAll(); echo $db->getLastQuery(); exit; }
//Output
SELECT COUNT(*) AS `numrows` FROM `heroes`
You can easily get the last executed query in Codeigniter 4 using the getLastQuery() method on the \Config\Database::connect(). This method returns the raw SQL query string, not the result.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: CodeIgniter
- How to create custom helper in codeigniter 4
- How to select all checkbox in Codeigniter
- How to get field names of table in Codeigniter
- How to load a view in CodeIgniter?
- Unable to set session in codeigniter
- How to call helper function in Codeigniter view
- Default htaccess file code Codeigniter
- Codeigniter 4 call model function from controller
- How to move from one view to another in Codeigniter
- Convert dd/mm/yyyy to yyyy-mm-dd in codeigniter
- No input file specified Codeigniter
- Get the version codeIgniter
- Update an existing table record with +1 in CodeIgniter
- Call to undefined function CodeIgniter\locale_set_default()