【问题标题】:exec store procedure with parameter date format getting error带有参数日期格式的 exec 存储过程出错
【发布时间】:2020-11-11 12:04:44
【问题描述】:

我正在使用 slim 框架来获取带有参数日期的 API。当我使用日期格式 'yyyy-mm-dd' 时出现错误。

这是代码:

 $time = strtotime('07/16/2020');
 $newDate = date('Y-m-d',$time);    
 $Poli    = $args['poli'];
 $sql = "Exec Proc_JKN_Rekap @Tgl=$newDate, @Poli= $Poli";   

错误:

消息: SQLSTATE[42000]:[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]不正确 '-' 附近的语法。

【问题讨论】:

    标签: php sql-server parameter-passing slim


    【解决方案1】:

    这个应该更靠谱

    // $inputDateString is whatever your function receives as date
    $date = \DateTime::createFromFormat('d/m/Y', $inputDateString);
    
    // validate the date
    // if the input date is invalid the comparison will fail
    // this works even for dates like February 31st
    if ( $date->format('d/m/Y') !== $inputDateString ) {
         throw new \InvalidArgumentException('Invalid date string: '.inputDateString);
    }
    
    
    $dateString = $date->format('Y-m-d');
    
    $Poli = $args['poli'];
    $sql  = "Exec Proc_JKN_Rekap @Tgl=$newDate, @Poli= $Poli";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-24
      • 2016-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多