Echo query before execution and without execution in codeigniter Active Record

后端 未结 5 1699
后悔当初
后悔当初 2020-12-08 10:43

I am looking for a way to see generated string of the query but without executing it.

Note that the query hasn\'t been executed before. (I do not wa

5条回答
  •  情话喂你
    2020-12-08 11:27

    I added this little method in DB_active_rec.php

    function return_query()
    {
        return $this->_compile_select();
    }
    

    Usage

    $this->db->select('id,user_name')->from('user')->where('id',1);
    
    $string =   $this->db->return_query();
    echo $string;
    

    Result

    SELECT `id`, `user_name` FROM (`user`) WHERE `id` = 1
    

    In this way you are bound to use

    $this->db->from()
    

    Instead of

    $this->db->get()
    

    Which runs the query

提交回复
热议问题