函数不返回所有数据库行

时间:2015-12-27 19:46:39

标签: php json database codeigniter

我在Code igniter PHP中有这个功能,我试图从数据库中获取与输入值匹配的所有行。 我应该如何更改代码以获取符合我的搜索条件的所有行?

谢谢

模型搜索功能:

function findquestion($searchvalue){

        $this->db->where('answer1', $searchvalue);
        $res = $this->db->get('questions');
        $count = $res->num_rows();

        if($res->num_rows()==0){
            return "...";
        }
        $moviearray = $res->row_array();
        return $moviearray;
    }

控制器功能:

public function getdatabasedata()
    {

        if($this->input->server('REQUEST_METHOD') == 'GET')
        {
            $year = $this->input->get('searchvalue');
            if($year != ''){
                $movie = $this->adminmodel->findquestion($year);
                echo json_encode($movie);
            } 
        }
}

当我看Postman检查JSON对象的内容时,这就是我得到的。但事实上,我的数据库中还有三行符合我的搜索条件。

{
  "id": "10",
  "question": "test questions",
  "image": "test image",
  "answer1": "answer1",
  "answer2": "answer2",
  "answer3": "answer3",
  "answer4": "answer4"
}

1 个答案:

答案 0 :(得分:0)

function findquestion($searchvalue){
  

1)定义数组。

        $data = array(); 
        $this->db->where('answer1', $searchvalue);
        $res = $this->db->get('questions');


        if($res->num_rows()==0){
            return "...";
        }
  

2)循环结果并存储在数组中。

        foreach ($res->result() as $row)
            {
                $data[] = array(
                    'id' => $row->id,
                    'question'  => $row->question,
                    'image'  => $row->image,
                    'answer1'  => $row->answer1,
                    'answer2'  => $row->answer2,
                    'answer3'  => $row->answer3,
                    'answer4'  => $row->answer4
                );
            }
        //$moviearray = $res->row_array();
        return $data;
    }