【问题标题】:Get last executed query in MySQL with PHP/CodeIgniter使用 PHP/CodeIgniter 在 MySQL 中获取最后执行的查询
【发布时间】:2011-09-26 01:23:26
【问题描述】:

如何获取我在 Windows 和 Linux 中的 MySQL 中运行的最后一个查询?

我正在使用 PHP 和 CodeIgniter。在my_model.php,我有:

$query1 = ( ...something... );
$query2 = ( ...something... );
$variables = ( .... something .... );
$this->db->query(" $query1 ... $variables .. $query2", array( $variables, ... ));

我需要在上面的代码 sn-p 之后执行最后一个查询。

谁能告诉我如何获得我的最后一个查询?

【问题讨论】:

  • 据我所知,没有通用的“获取最后一个查询”功能。你需要它做什么?
  • @Pekka,CodeIgniter 有这个内置的 :-)
  • @Rocket 啊,很公平。
  • 如果您正在调试,其他有用的功能是 mysql_errno(); // 返回最后一次 MySQL 操作的错误号 3 mysql_error(); // 返回上一次 MySQL 操作的错误描述 mysql_info(); // 返回上次查询的信息

标签: php mysql codeigniter


【解决方案1】:

用途:

$this->db->last_query();

Returns the last query that was run (the query string, not the result). Example:
$str = $this->db->last_query();

// Produces: SELECT * FROM sometable.... 

取自query helper functions手册中的示例

【讨论】:

  • 得到这个:PHP 致命错误:未捕获的错误:调用未定义的方法 mysqli::last_query()
  • @AhmadAmeri 请注意,答案是指 9 年前的 Codeignter 版本和 PHP。
  • 您知道如何获取最新查询吗?
【解决方案2】:

在 CodeIgniter 中有一个辅助函数 $this->db->last_query();

这将返回最后一个查询的字符串。

但我想你可能会问如何获得结果,在这种情况下你会这样做:

$query1 = ( ...something... );
$query2 = ( ...something... );
$variables = ( .... something .... );
$query = $this->db->query(" $query1 ... $variables .. $query2", array( $variables, ... ));

foreach ($query->result() as $row) {
//code goes here
}

如需了解更多信息,请查看CI's examples

【讨论】:

    【解决方案3】:

    在 config/database.php 中,必须设置配置数组:'save_queries' => TRUE,如果为 false,则 last_query() 函数不工作。

    【讨论】:

      猜你喜欢
      • 2011-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-31
      • 1970-01-01
      • 1970-01-01
      • 2015-12-01
      • 2011-08-12
      相关资源
      最近更新 更多