codeigniter-mysql带变量查询

8e2ybdfx  于 2021-06-18  发布在  Mysql
关注(0)|答案(6)|浏览(304)

我需要帮助在我的模型中查询,我想找到一个变量,我输入到控制器的字段,至于现在的问题是,该字段不会读取我的变量,所以这里是代码
控制器

public function tampil_soal(){
        $kode_soal = $this->input->post('kode_soal');
        $where = array('kode_soal' => $kode_soal);
        $data['tampilan']= $this->m_model->tampil_soal($kode_soal)->result();
        $this->load->view('soal_tampil',$data);
    }

模型

public function tampil_soal($where){
        return $this->db->query("select * from soal where kode_soal='$where' ORDER BY RAND()");
    }

看法

<form action="<?php echo base_url()?>siswa/tampil_soal" method="post" class="form">
    <input class="input" name="kode_ujian" placeholder="Kode ujian"/>
  <input class="button" type="submit" name="submit" value="Selesai"/>
</form>
ntjbwcob

ntjbwcob1#

请尝试以下查询:

public function tampil_soal(){
        $data['tampilan']= $this->m_model->tampil_soal($this->input->post('kode_soal');
        $this->load->view('soal_tampil',$data);
    }

//型号

public function tampil_soal($where){
      return $this->db->where('kode_soal',$where)->get('soal')->result();
    }
yzuktlbb

yzuktlbb2#

模型

public function tampil_soal($where){
    $condition = $where;
    // Your Condition...
    $this->db->where($condition);
    // Order By ...
    $this->db->order_by('RAND()');
    // Return Data from Table ...
    return $this->db->get('soal');    
}
x0fgdtte

x0fgdtte3#

改变这个

public function tampil_soal($where){
        return $this->db->query("select * from soal where kode_soal='$where' ORDER BY RAND()");
    }

为了这个

public function tampil_soal($kode_soal){
        return $this->db->query("select * from soal where kode_soal='$kode_soal' ORDER BY RAND()");
    }

希望有帮助

ut6juiuv

ut6juiuv4#

试试这个:

//controller
public function tampil_soal(){
    $kode_soal = $this->input->post('kode_ujian');
    $data['tampilan']= $this->m_model->tampil_soal($kode_soal);
    $this->load->view('soal_tampil',$data);
}

// model
public function tampil_soal($where){
    $this->db->where('kode_soal',$where);
    return $this->db->get('soal')->result();
    // use the return $this->db->get('soal')->row();
    // if your query return one record
}
0g0grzrc

0g0grzrc5#

我认为必须在查询中使用result()函数

public function tampil_soal($where){
        return $this->db->query("select * from soal where kode_soal='$where' ORDER BY RAND()")->result();
    }
h7wcgrx3

h7wcgrx36#

看法

<form action="<?php echo base_url()?>siswa/tampil_soal" method="post" class="form">
    <input class="input" name="kode_ujian" placeholder="Kode ujian"/>
  <input class="button" type="submit" name="submit" value="Selesai"/>
</form>

控制器
您使用了错误的post名称“$this->input->post('kode\u soal')”,请将其更改为$this->input->post('kode\u ujian'),然后

public function tampil_soal(){
        $kode_soal = $this->input->post('kode_ujian');
        $where = array('kode_soal' => $kode_soal);
        $data['tampilan']= $this->m_model->tampil_soal($kode_soal);
        $this->load->view('test',$data);
    }

模型

public function tampil_soal($where){
    $qry = $this->db->query("select * from soal where kode_soal='$where' ORDER BY RAND()");
        if($qry->num_rows()>0){
            return $qry->result();
        }else{
            return array();
        }
    }

相关问题