6

I just started learning Codeigniter 4. My query always generates NULL and I don't know why. How can I see the generated SQL Select command just like Codeigniter 3?

In Codeigniter 3 this command does the job:

echo $this->db->last_query();

And this is my controller code in Codeigniter 4 that I need to get the generated query:

$cityModel = new CityModel();
$cities = $cityModel
    ->select('city.name AS cityName')
    ->select('county.name AS countryName')
    ->select('province.name AS provinceName')
    ->join('province', 'city.province_id = province.id', 'left')
    ->join('county', 'city.county_id = county.id', 'left')
    ->result();

Update: I tried this code but it's returning an empty string:

var_export((string)$cityModel->db->getLastQuery());
2
  • "My query always generates NULL"? maybe county.name should be country.name. "What's the $db? It's not defined in the controller"? you could define it like this: $db=$this->db; Commented Jan 31, 2020 at 16:47
  • echo $this->db->getLastQuery(); Commented Jul 7, 2021 at 12:52

8 Answers 8

14

This should display the final query:

$cityModel->getLastQuery()->getQuery()
Sign up to request clarification or add additional context in comments.

Comments

5

In CI 4 Refer Doc

you can use getLastQuery() as

$query = $db->getLastQuery();
echo (string)$query;

2 Comments

What's the $db? It's not defined in the controller.
use $this->db to get the last query.
4

This worked for me

$myModel = new MyModel();// your any model
    
$myModel->db->getLastQuery()->getQuery();

$this->db is not directly accessible from controller, but if you have any Model instance then you can have the full access to db object.

3 Comments

What's the difference between this approach and Piyush's answer from September? Doesn't $this translate to $myModel in this case?
In My Case, $this->db was not accessible from Controller, I need to create instance for it. But I tried with my Model instance and it worked.
That's useful to know. I'd recommend editing your answer to include that information so that it's clear upfront how it differs from Piyush's answer.
2

This code will help you to get the last query in codeigniter 4.

 $this->db = \Config\Database::connect();
 $data = $builder->get()->getResult();
 echo $this->db->getLastQuery(); die;

Comments

0

You can use getCompiledSelect it will return the query SELECT command.

$sql = $cityModel->getCompiledSelect();
echo $sql;

2 Comments

in your case it will be $cityModel->getCompiledSelect(); it attaches with $this->db->getCompiledSelect()
Not work for a join query?
0

Following Code will work in CI 4

$this->db->getLastQuery()->getQuery();

1 Comment

Worked for me, it returning the query.
-1

Add this code in your class

$db = \Config\Database::connect();
$query = $db->getLastQuery();
echo $query;

1 Comment

Won't this make a new connection, which doesn't yet have a "last query"?
-1

Simply just use $this->db->getLastQuery();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.