我在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变为回声?
答案 0 :(得分:2)
$result
是associative
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)
答案 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