php 在执行前和未在 codeigniter Active Record 中执行时回显查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9010128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:59:03  来源:igfitidea点击:

Echo query before execution and without execution in codeigniter Active Record

phpmysqlcodeigniteractiverecord

提问by Mohammad Naji

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

我正在寻找一种方法来查看生成的查询字符串但执行它。

Note that the query hasn't been executed before. (I do not want $this->db->last_query();)

请注意,该查询之前未执行过。(我不想$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 THATget()executes the code, but echo_query_string()just echoes the string of query without execution.

我希望有一种方法的名称就像$this->db->echo_query_string($table_name = '');使用完全一样,$this->db->get($table_name = '');但唯一的区别是get()执行代码,但echo_query_string()只是在不执行的情况下回显查询字符串。

回答by Stewie

You can see the compiled query by either of these functions

您可以通过这些函数中的任何一个查看已编译的查询

/* SELECT */ $this->db->_compile_select();
/* INSERT */ $this->db->_insert();
/* UPDATE */ $this->db->_update();

回答by BrCoder08

You don't need to change any file in codeigniter because it already provides a method to do that.

您不需要更改 codeigniter 中的任何文件,因为它已经提供了一种方法来做到这一点。

Using

使用

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

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

will produce

会产生

select * from some_table...

select * from some_table...

And this is it.

就是这样。

回答by Muhammad Raheel

I added this little method in DB_active_rec.php

我在 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

哪个运行查询

回答by Amitabh Das

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

从 Codeigniter 的第 3 版开始,请参考这个url 和这个.

  • 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();
  • echo $this->db->update_string();或者 echo $this->db->get_compiled_update();
  • echo $this->db->insert_string();或者 $this->db->get_compiled_insert();
  • echo $this->db->get_compiled_delete();
  • echo $this->db->get_compiled_select();

回答by Samuel Dauzon

You can use some public methodsto get SQL queries

可以使用一些公共方法来获取 SQL 查询

Get a SELECT query

获取 SELECT 查询

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

Get a INSERT query

获取 INSERT 查询

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

Get a UPDATE query

获取 UPDATE 查询

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

Get a DELETE query

获取 DELETE 查询

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