查询执行是否发生在以下 codeigniter 事件记录语句的 get_where()
子句处?
$this->db->select('*');
$q = $this->db->get_where('Contacts', array('id' => $contact_id));
$sql = $this->db->last_query();
或者一旦您调用 result_array()
就会发生这种情况吗?
$this->db->last_query();
是获取查询字符串的可靠方法。
最佳答案
查询执行发生在所有 get 方法上,例如
$this->db->get('table_name');
$this->db->get_where('table_name',$array);
虽然last_query包含最后运行的查询
$this->db->last_query();
如果您想获取查询字符串而不执行,则必须这样做。 转到 system/database/DB_active_rec.php 从这些函数中删除 public 或 protected 关键字
public function _compile_select($select_override = FALSE)
public function _reset_select()
现在您可以编写查询并将其获取到变量中
$this->db->select('trans_id');
$this->db->from('myTable');
$this->db->where('code','B');
$subQuery = $this->db->_compile_select();
现在重置查询,这样如果您想编写另一个查询,该对象将被清除。
$this->db->_reset_select();
事情就这样完成了。干杯!!! 注意:使用这种方式时,您必须使用
$this->db->from('myTable')
而不是
$this->db->get('myTable')
运行查询。
关于Codeigniter:执行 $this->db->last_query();执行查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15953811/