Codeigniter - 来自session_userdata的Get_where基础

时间:2017-09-27 08:55:29

标签: php sql codeigniter

我想选择emp id与我的会话userdata(emp_id)匹配的所有行。这是我的代码。我收到很多错误,没有选择任何行。请有人帮帮我。感谢

型号:

public function get_save_samp($emp_id) {
   $query = $this->db->get_where('tblsavesample', array('emp_id' => 
       $emp_id));
   return $query->row_array();
}

控制器:

public function show() {

    $emp_id = $this->session->userdata('emp_id');
    $data['save'] = $this->user_model->get_save_samp($emp_id);
    $this->load->view('show',$data);
}

查看:

<?php foreach ($save as $row) { ?>                                                                                      
    <td style="width: " ><?php echo $row->emp_id ?></td>
    <td style="width: " ><?php echo $row->emp_code?></td>
    <td style="width: " ><?php echo $row->emp_name ?></td>
<?php   }>

5 个答案:

答案 0 :(得分:2)

试试这个

public function get_save_samp($emp_id) {
       $query = $this->db->get_where('tblsavesample', array('emp_id' => 
       $emp_id));
        return $query->result();
       }

答案 1 :(得分:2)

你可以尝试解决你的问题:

更改模态函数:

  

Modal.php

public function get_save_samp($emp_id) {
     $this->db->select("*"); 
     $this->db->where('id', $emp_id);
     $this->db->get('tblsavesample');
     return $query->row();
}
  

查看:

<?php foreach ($save as $row) {?>
   <td style="width: " ><?php echo $row->emp_id ?></td>
   <td style="width: " ><?php echo $row->emp_code ?></td>
   <td style="width: " ><?php echo $row->emp_name ?></td>
<?php } ?>

答案 2 :(得分:0)

更改您的模态代码:

  public function get_save_samp($emp_id) {
     //$query = $this->db->get_where('tblsavesample', array('emp_id' =>$emp_id));
     $this->db->select("*"); 
     $this->db->where_in('id', $emp_id);
     $query = $this->db->get('tblsavesample');
     return $query->result();
  }

并像这样更改您的控制器代码,以检查我们在控制器中获取的数据。

public function show()
{    
    $emp_id       = $this->session->userdata('emp_id');
    $data['save'] = $this->user_model->get_save_samp($emp_id);
    echo "<pre>";
    print_R($data['save']);
    exit();
    $this->load->view('show', $data);
}

答案 3 :(得分:0)

row_array()仅返回第一行。如果要返回所有记录,请改用result_array()。

$result = $query->result_array();
return $result;

链接:click

答案 4 :(得分:0)

row_array()用于仅返回单个记录。按如下方式更改模型中的代码:

public function get_save_samp($emp_id) {  

$query = $this->db->get_where('tblsavesample', array('emp_id' => 
   $emp_id));

return $query->result_array();

}

并检查控制器中返回的记录数,如下所示:

$data['save'] = $this->user_model->get_save_samp($emp_id);
echo $data['save']->num_rows();

如果显示0,则表示数据库表中没有匹配的记录。

测试查询,尝试在mysql中执行相同的查询