简体   繁体   English

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

[英]How to pass date as a parameter to MySQL Stored Procedure in PHP CodeIgniter?

I'm trying to create a Date-Range-Filtration in my web app. 我正在尝试在我的网络应用中创建Date-Range-Filtration For that I'm taking an input using date-picker and passing that to my controller to model . 为此,我使用date-picker进行输入并将其传递给我的controller to model When I'm echo it out it gives me the correct date value that I've selected from date-picker . 当我回应它时,它给了我从date-picker选择的正确日期值。 But when I'm running my code, it gives me blank output. 但是当我运行我的代码时,它会给我空白输出。 I'm wondering what is wrong with my code. 我想知道我的代码有什么问题。

Here is my SP code: 这是我的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 ;

Here is my Controller code: 这是我的控制器代码:

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

Here is my Model code: 这是我的模型代码:

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

I've googled it out, but I'm stuck, any kind of help is welcome, Thanks in advance. 我已经用Google搜索了,但是我被卡住了,欢迎提供任何帮助,谢谢。

Try this: 试试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM