PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 1, 2022

[FIXED] What's the equivalent of $this->db->last_query() in Codeigniter 4?

 January 01, 2022     activerecord, codeigniter, codeigniter-4, php     No comments   

Issue

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());

Solution

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

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


Answered By - Fawad Saboor
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing