【问题标题】:Passing date to mysql stored procedure from codeigniter从codeigniter将日期传递给mysql存储过程
【发布时间】:2017-04-22 15:12:46
【问题描述】:

我创建了以下正常工作的存储过程。

CREATE DEFINER=`root`@`localhost` PROCEDURE `SPTeam`(IN date_x date, OUT  emplist3 varchar(200))
BEGIN
select employee.name as emplist3
from employee
where employee.id IN (
  select emp_position.employee_id
  from emp_position
  where emp_position.employee_id NOT IN (
     select emp_event.employee_id
     from emp_event
     where emp_event.date = date_x
  )
  AND emp_position.position = "Cameraman"
 );
END

我在codeigniter模型中通过以下代码调用这个存储过程。

  $SQL = "call SPTeam("
    . $date.","
    . " @emplist3" //output
    .");";

$this->db->trans_start();
    $query = $this->db->query($SQL); 
    $this->db->trans_complete();
$result = array();
foreach($query->result() as $rows){
    $result[]=$rows;
}
return $query->result();
}

在我的控制器中,我将 $date 传递给上述模型函数,如下所示。

    $date = '06/12/2016';
    $data= $this->employee_model->sp_call($date) ;

但它给出了错误的结果。 我也尝试了以下日期格式。

    $date = '2016-12-14';
    $data= $this->employee_model->sp_call($date) ;

它给出了以下错误。

Error Number: 1292

Incorrect date value: '1990' for column 'date_x' at row 1

call SPTeam(2016-12-14, @emplist3);

请有人帮我纠正这个问题。

【问题讨论】:

    标签: mysql codeigniter stored-procedures


    【解决方案1】:

    这可能是一些不同的事情。首先,确保传入 SP 的字符串值是 YYYY-MM-DD 格式,因为这是 MySQL 中的 Date 列。

    此外,SP 可能要求传入的日期字符串在其周围加上单引号,以便正确处理。

    试试这样的:

    $date = "'2016-12-14'";
    

    或者这个(注意添加了单引号):

     $SQL = "call SPTeam('"
        . $date."',"
        . " @emplist3" 
        .");";
    

    如果这不起作用,您可能需要调整您的 SP 以在 MySQL 中使用 STR_TO_DATE function。由于您在技术上是在传递一个字符串,因此您可能需要确保对其进行相应的格式化。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2014-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-13
      • 2011-02-13
      • 2017-02-19
      • 2021-11-15
      • 2013-11-25
      相关资源
      最近更新 更多