codeigniter-mysql带变量查询

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

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

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>
vngu2lb8

vngu2lb81#

请尝试以下查询:

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();
    }
w1jd8yoj

w1jd8yoj2#

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

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

8mmmxcuj3#

试试这个:

//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
}
ej83mcc0

ej83mcc04#

看法

<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();
        }
    }
ql3eal8s

ql3eal8s5#

改变这个

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()");
    }

希望有帮助

bz4sfanl

bz4sfanl6#

模型

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');    
}

相关问题