如何将日期作为参数传递给PHP CodeIgniter中的MySQL存储过程?

时间:2018-02-22 11:43:26

标签: php mysql stored-procedures parameters codeigniter-3

我正在尝试在我的网络应用中创建Date-Range-Filtration。为此,我使用date-picker进行输入并将其传递给我的controller to model。当我回应它时,它给了我从date-picker中选择的正确日期值。但是当我运行我的代码时,它会给我空白输出。我想知道我的代码有什么问题。

这是我的SP代码:

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `testDefects`(IN `startdate` DATE, IN `enddate` DATE)
    NO SQL
    DETERMINISTIC
BEGIN
SELECT `defect_id`, `user_id`, `defect_rooms`, `defect_start_date`, `defect_subject`, `defect_responsibility`, `defect_time_frame`, `defect_followup_date`, `defect_assign_to`, `defect_notes`, `defect_details`, `defect_status`, `defect_updated_date` FROM `tbl_defects` WHERE defect_followup_date BETWEEN startdate AND enddate;
END$$
DELIMITER ;

以下是我的控制器代码:

public function DefectFilterTest()
    {
        # code...
        $user = $this->ion_auth->user()->row();
        $data['username'] = $user->username;

        $data['user_id'] = $user->id;
        $user_id = $user->id;
        $data['groupId'] = $this->l->groupId($user_id);
        $data['group'] = $data['groupId']['0']->group_id;
        $data['title'] = 'Property Defects List';

        $startdate = $this->input->post('fromdate');
        $enddate = $this->input->post('todate');

        if($startdate == ''){
            $startdate ='1900-01-01';
        }
        if($enddate == ''){
            $enddate ='2100-01-01';
        }
        echo $startdate. "<br>";
        echo $enddate. "<br>";

        $data['defectslist'] = $this->p->testDefects($startdate, $enddate);

        echo "<pre>";
        print_r($data['defectslist']);
        echo "</pre>";
        exit();

        $this->load->view('template/header', $data); 
        $this->load->view('Property/defectList', $data); 
        $this->load->view('template/footer'); 
    }

以下是我的模型代码:

public function testDefects($startdate, $enddate)
    {
        # code...   testDefects
        $query = $this->db->query("call testDefects($startdate, $enddate)");

        if ($query->num_rows()) {
            $data = $query->result();
            $query->next_result(); 
            $query->free_result();
            return $data;
        }else{
            return false;
        }
    }

我已经搜索过了,但是我被卡住了,欢迎任何形式的帮助,提前致谢。

1 个答案:

答案 0 :(得分:3)

试试这个:

# code...   testDefects
$query = $this->db->query("call testDefects('$startdate', '$enddate')");