How to print SQL statement in codeigniter model

前端 未结 14 1802
迷失自我
迷失自我 2020-11-30 22:08

I have a sql statement in my model,

I then say

$query = $this->db->query($sql, array(fields, fields1);

if ($query) {
    return true:
} else {         


        
相关标签:
14条回答
  • 2020-11-30 22:31

    use get_compiled_select() to retrieve query instead of replace it

    0 讨论(0)
  • 2020-11-30 22:33

    To display the query string:

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

    To display the query result:

    print_r($query);
    

    The Profiler Class will display benchmark results, queries you have run, and $_POST data at the bottom of your pages. To enable the profiler place the following line anywhere within your Controller methods:

    $this->output->enable_profiler(TRUE);
    

    Profiling user guide: https://www.codeigniter.com/user_guide/general/profiling.html

    0 讨论(0)
  • 2020-11-30 22:37

    Add this line right after the query you want to print.

    Example:

    $query = $this->db->query('SELECT * FROM table WHERE condition');

    //Add this line.

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

    exit();

    or

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

    0 讨论(0)
  • 2020-11-30 22:39

    I try to @Chumillas's answer and @chhameed's answer, but it not work,because the sql is wrong.So I found new approach,like this:

    • Insert echo $sql; flush(); exit; into before return $sql; _compile_select function of DB_active_rec.php
    0 讨论(0)
  • 2020-11-30 22:40

    Neither last_query() or get_compiled_select() works for me, so a slight change of pedro's code works for me just fine. Do not include ->get() in your build, this must be before the ->get()

     echo $this->EE->db->_compile_select();
    
    0 讨论(0)
  • 2020-11-30 22:41

    I had exactly the same problem and found the solution eventually. My query runs like:

    $result = mysqli_query($link,'SELECT * FROM clients WHERE ' . $sql_where . ' AND ' . $sql_where2 . ' ORDER BY acconame ASC ');
    

    In order to display the sql command, all I had to do was to create a variable ($resultstring) with the exact same content as my query and then echo it, like this:<?php echo $resultstring = 'SELECT * FROM clients WHERE ' . $sql_where . ' AND ' . $sql_where2 . ' ORDER BY acconame ASC '; ?>

    It works!

    0 讨论(0)
提交回复
热议问题