如何回显mysql选择结果codeigniter

时间:2017-02-28 04:43:05

标签: mysql codeigniter

我在codeigniter中编写了以下mysql查询

  $query = $this->db->query("SELECT count(`leave_status`) FROM `teacher_attendance` WHERE `teacher_id`='20' AND `leave_status`='6'");
    if($query->num_rows() > 0){
          $result = $query->row_array();
          print_r($result);
          echo $result;
    }               

with echo $ result;什么都不打印; with print_r($ result);数组([count(leave_status)] => 7)像这样的数组

但是如何将这个值7变为回声?

5 个答案:

答案 0 :(得分:2)

$resultassociative array您不能直接echo它。使用leave_status访问$result['leave_status']的值。如下所示。

$query = $this->db->query("SELECT count(`leave_status`) AS leave_status FROM `teacher_attendance` WHERE `teacher_id`='20' AND `leave_status`='6'");
    if($query->num_rows() > 0){
          $result = $query->row_array();
          //print_r($result);
          $total = $result['leave_status'];
          echo $total ;//prints 7
    }  

答案 1 :(得分:1)

您可以使用row()来回显单行结果

sequelize db:migrate

请参阅this link了解更多信息

答案 2 :(得分:0)

使用foreach

$query = $this->db->query("SELECT count(`leave_status`) as count FROM `teacher_attendance` WHERE `teacher_id`='20' AND `leave_status`='6'");

if($query->num_rows() > 0){
   foreach ($query->result_array() as $row) {
      echo $row['count'];
   }
}

答案 3 :(得分:0)

试试这个

$query = $this->db->query("SELECT count(`leave_status`) as count FROM `teacher_attendance` WHERE `teacher_id`='20' AND `leave_status`='6'");
if($query->num_rows() > 0){
      $result = $query->row_array();
      print_r($result);
      echo $result['count'];
}   

答案 4 :(得分:0)

在您的模型中:返回$ result

在控制器中: $ data = $ this-> your_model-> your_function();

在视图中:echo $ leave_status