首页 > 解决方案 > 我希望我的代码从存储在数据库中的列中请求特定值

问题描述

class quizmodel extends CI_Model {

    public function getQuestions()
    {
        $this->db->select("quizID, question, choice1, choice2, choice3, answer, subject");
        $this->db->from("quiz");
        $this->db->where?????


        $query = $this->db->get();

        return $query->result();

        $num_data_returned = $query->num_rows;

        if ($num_data_returned < 1) {
          echo "There is no data in the database";
          exit();   
        }
    }

这是我的代码,我想要它以便“主题”字段只要求“计算”。或者“quizID”要求从 1 到 10 的 quizID。

标签: phpcodeigniter

解决方案


如果你想获得quizID1-10 范围,那么你可以使用如下范围:

$this->db->where('quizID >=', 1);
$this->db->where('quizID <=', 10);

或者,

$this->db->where("quizID BETWEEN 1 AND 10");

如果您只想获取computing相关数据,则可以LIKE在此处使用:

$this->db->like('subject', 'computing');

一些额外和有用的信息:

$this->db->like('subject', 'computing', 'before');    // Produces: WHERE `subject` LIKE '%computing'
$this->db->like('subject', 'computing', 'after');     // Produces: WHERE `subject` LIKE 'computing%'
$this->db->like('subject', 'computing', 'none');      // Produces: WHERE `subject` LIKE 'computing'
$this->db->like('subject', 'computing', 'both');      // Produces: WHERE `subject` LIKE '%computing%'

推荐阅读