28

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 want $this->db->last_query();)

I hope there be a method with a name like $this->db->echo_query_string($table_name = ''); to be used exactly like $this->db->get($table_name = ''); BUT THE ONLY DIFFERENCE BE THAT get() executes the code, but echo_query_string() just echoes the string of query without execution.

1
  • 1
    A simple - but probably not so effective - way I would do is slightly modify the query to make an erroneous query such that the system displays the error along with the simple expected error & it will not be executed. Commented Sep 27, 2014 at 15:27

4 Answers 4

29

You can see the compiled query by either of these functions

/* SELECT */ $this->db->_compile_select();
/* INSERT */ $this->db->_insert();
/* UPDATE */ $this->db->_update();
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you. Does that work just for select queries? Isn't there anything exactly instead of get()? I hope it be able it to have an optional $database_name like get()
For insert its $this->_insert() for update its $this->_update() But these two require few arguments
In the newest CodeIgniter version $this->db->_compile_select() is protected, and cannot be called. You need to use $this->db->get_compiled_select() (which may or not be in the latest stable version, it's in their dev version).
You also need to use get_compiled_insert and get_compiled_update instead.
@smhnaji: I think _compile_select still works in 2.0.3, but that's not the most stable. The most stable version is 2.1.0 which removes _compile_select. They added get_compiled_select, but that's not in the stable version, it's only in the development version.
|
11

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

Comments

5

You can use some public methods to get SQL queries

Get a SELECT query

$sql = $this->db->get_compiled_select();

Get a INSERT query

$sql = $this->db->get_compiled_insert();

Get a UPDATE query

$sql = $this->db->get_compiled_update();

Get a DELETE query

$sql = $this->db->get_compiled_delete();

Comments

4

As of version 3 of Codeigniter, please refer to this url and also to this.

  • echo $this->db->update_string(); OR echo $this->db->get_compiled_update();
  • echo $this->db->insert_string(); OR $this->db->get_compiled_insert();
  • echo $this->db->get_compiled_delete();
  • echo $this->db->get_compiled_select();

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.