无法使用codeigniter从db中检索数据

时间:2017-05-11 13:06:45

标签: php mysql codeigniter

我需要使用codeigniter中的“get_where”短语从数据库中获取数据。

构建的预期查询是:select * from tele_data where F_NAME="" and M_NAME="" and L_NAME="" and FACTORY="" and DEPT="" and COMPANY_NAME="" and DESIGNATION=""

我的观点

<div class="form-row">
            <label>
                <span style="color:#ff4d4d">Factory Name :</span>
                <select name="fcode" id="fcode">
                        <option value="CMGM">Crystal Martin Ceylon - Malwatta</option>
                        <option value="CMW">Crystal Martin Ceylon - Wathupitiwala</option>
                        <option value="CMG">Crystal Martin Ceylon - Garments</option>
                        <option value="CME" selected>Crystal Martin Ceylon - Europe</option>
                        <option value="CMCG">Crystal Martin Central - Galagedara</option>
                        <option value="CMPK">Crystal Martin Ceylon - Kanthale</option>
                        <option value="CMCD">Crystal Martin Central - Dambulla</option>
                </select>
            </label>
        </div>

        <div class="form-row">
            <label>
                <span>First Name :</span>
                <input type="text" name="fname" id="fname" >
            </label>
        </div>

        <div class="form-row">
            <label>
                <span>Middle Name :</span>
                <input type="text" name="mname" id="mname">
            </label>
        </div>

        <div class="form-row">
            <label>
                <span>Last Name :</span>
                <input type="text" name="lname" id="lname">
            </label>
        </div>

        <div class="form-row">
            <label>
                <span>Department :</span>
                <select name="department" id="department">
                    <option selected>Select One</option>
                    <option value="IT">Information Technology (IT)</option>
                    <option value="HR">Human Resources & Administration</option>
                    <option value="IE">Work Study</option>
                    <option value="QA">Quality Assurance & Control</option>
                    <option value="LEAN">Lean</option>
                    <option value="GENERAL">General</option>
                    <option value="OTHER">Other</option>
                </select>
            </label>
        </div>

        <div class="form-row">
            <label>
                <span>Company Name :</span>
                <input type="text" name="compname" id="compname">
            </label>
        </div>

        <div class="form-row">
            <label>
                <span>Designation :</span>
                <input type="text" name="designation" id="designation">
            </label>
        </div>

        <div class="form-row">
            <button type="submit">Search Now</button>
        </div>

    </form>

我给出了工厂名称=“CMG”&amp;部门=“IT”作为表格的输入。其他人是空白的。

我的控制器

public function search_results()
{
        $sdata = [

                'FACTORY'=>$this->input->post('fcode'),
                'F_NAME'=>$this->input->post('fname'),
                'M_NAME'=>$this->input->post('mname'),
                'L_NAME'=>$this->input->post('lname'),
                'DEPT'=>$this->input->post('department'),
                'COMPANY_NAME'=>$this->input->post('compname'),
                'DESIGNATION'=>$this->input->post('designation')

                ];
        $this->load->model('users');
        $y=$this->users->display_users($sdata);

        //var_dump($y);die;
            if($y==TRUE){

                $data["display_Data"]=$y;
                $this->load->view("results/find",$data);
            } else {

                redirect("/index/home");
            }

    }

控制器捕获输入并成功将数据传递给模型

我的模特

function display_users($sdata){

        $query=$this->db->get_where('tele_data',$sdata);

        $result=$query->result_array();
        var_dump($result);die;

        if($query->result_array()==TRUE){

            foreach ($query->result_array() as $vals){


                    echo $vals['SD'];
                    echo $vals['EXT'];
                    echo $vals['F_NAME'];
                    echo $vals['M_NAME'];
                    echo $vals['L_NAME'];
                    echo $vals['FACTORY'];
                    echo $vals['DEPT'];
                    echo $vals['COMPANY_NAME'];
                    echo $vals['DESIGNATION'];

            }



        return $query->result_array();


        } else {

            return FALSE;
        }

    }

但是var_dump命令给了我一个空数组作为结果。这是什么原因?我附上了一张db的图片供你参考。但是当我在MySql中单独运行查询时,它给了我预期的结果。see the db image请帮我快点解决。

3 个答案:

答案 0 :(得分:1)

我的控制器

试试这个

$this->db->select('*');
        $this->db->from('tele_data');
        $this->db->where($sdata);
$query = $this->db->get();
    return $query->result_array();

答案 1 :(得分:1)

改变数据数组部分,如下面的

$sdata = [

            'FACTORY'=>isset($this->input->post('fcode'))?$this->input->post('fcode'):'',
            'F_NAME'=>isset($this->input->post('fname'))?$this->input->post('fname'):'',
            'M_NAME'=>isset($this->input->post('mname'))?$this->input->post('mname'):'',
            'L_NAME'=>isset($this->input->post('lname'))?$this->input->post('lname'):'',
            'DEPT'=>isset($this->input->post('department'))?$this->input->post('department'):'',
            'COMPANY_NAME'=>isset($this->input->post('compname'))?$this->input->post('compname'):'',
            'DESIGNATION'=>isset($this->input->post('designation'))?$this->input->post('designation'):''

            ];

答案 2 :(得分:-1)

的var_dump($结果);模具;这是什么    您已将模具功能添加到模型代码

 function display_users($sdata){

            $query=$this->db->get_where('tele_data',$sdata);

            $result=$query->result_array();
           // var_dump($result);die;

            if($query->result_array()==TRUE){

                foreach ($query->result_array() as $vals){


                        echo $vals['SD'];
                        echo $vals['EXT'];
                        echo $vals['F_NAME'];
                        echo $vals['M_NAME'];
                        echo $vals['L_NAME'];
                        echo $vals['FACTORY'];
                        echo $vals['DEPT'];
                        echo $vals['COMPANY_NAME'];
                        echo $vals['DESIGNATION'];

                }



            return $query->result_array();


            } else {

                return FALSE;
            }

        }