隐含选择Codeigniter中的Count并回显结果

时间:2018-09-18 13:36:43

标签: php sql codeigniter

我要在CodeIgniter中实现以下查询:

SELECT COUNT(*) FROM tickets WHERE status = "open";

返回的结果将为'1',我想回显结果。我有当前的代码查询:

$this->db->count_all_results();
$this->db->select('*');
$this->db->where('status', 'Open');
$this->data['opentickets'] = $this->support_m->get();

我正在尝试在视图中显示计数结果。关于如何执行此操作的任何建议?

2 个答案:

答案 0 :(得分:3)

请尝试下面的代码以获取打开状态下的行数。

$this->db->where("status", 'Open');
$query = $this->db->get("tickets");
$this->data['opentickets'] = $query->num_rows();

或者您可以使用这个

$sql = 'SELECT COUNT(*) FROM tickets WHERE status = "open"';
$query = $this->db->query($sql);
$this->data['opentickets'] =  $query->row_array()['COUNT(*)'];

答案 1 :(得分:0)

您可以在codeigniter中运行查询:

$sql = 'SELECT COUNT(*) FROM tickets WHERE status = "open"';
$query = $this->db->query($sql);
$this->data['opentickets'] = $query->result_array();