dq_1984
2015-08-21 19:19
浏览 58
已采纳

Yii2 - HY093中的查询错误

I need to get people who has birthday today, yesterday and the day before yesterday.

I'm using yii2 and postgresql, but I'm getting the error

SQLSTATE[HY093]: Invalid parameter number: :dayBind
Failed to prepare SQL: select name from employee
where date_part('day', born_date) = ':dayBind' and date_part('month', born_date) = ':monthBind'
Error Info: Array
(
    [0] => HY093
    [1] => 0
)

I don't know what I'm doing wrong in this code

 $days = array('day before yesterday' => date('m/d',  strtotime("-2 days"))
             , 'yesterday' => date('m/d',  strtotime("-1 days"))
             , 'today' => date('m/d')
 );

 $sql = "select name from employee
            where date_part('day', born_date) = ':dayBind' and date_part('month', born_date) = ':monthBind'";

 $result = array();
 foreach($days as $definition => $date) {

        list($m, $d) = explode("/", $date);

        $params = array(':dayBind' => $d, ':monthBind' => $m);

        $result[$definition] = Yii::$app->db->createCommand($sql)->bindValues($params)->queryAll();
 }

图片转代码服务由CSDN问答提供 功能建议

我需要得到今天,昨天和前天生日的人。 \ n

我正在使用yii2和postgresql,但是我收到了错误

  SQLSTATE [HY093]:参数号无效:: dayBind 
无法准备SQL: 从employee 
其中选择姓名date_part('day',born_date)=':dayBind'和date_part('month',born_date)=':monthBind'
错误信息:数组
(
 [0] => HY093  
 [1] => 0 
)
   
 
 

我不知道我在这段代码中做错了什么 \ n

  $ days = array('day before''=> date('m / d',strtotime(“ -  2天”))
,'yesterday'=> date('m  / d',strtotime(“ -  1天”))
,'今天'=>日期('m / d')
); 
 
 $ sql =“从员工中选择姓名
其中date_part  ('day',born_date)=':dayBind'和date_part('month',born_date)=':monthBind'“; 
 
 $ result = array(); 
 foreach($ days as $ definition =>  ; $ date){
 
 list($ m,  $ d)= explode(“/”,$ date); 
 
 $ params = array(':dayBind'=>  $ d,':monthBind'=>  $ m); 
 
 $ result [$ definition] = Yii :: $ app-> db-> createCommand($ sql) - > bindValues($ params) - > queryAll(); 
}  
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dpziir0079 2015-08-24 02:18
    最佳回答

    Remove single quote marks on bind param.
    Like this :

    $sql = "select name from employee where date_part('day', born_date) = :dayBind and date_part('month', born_date) = :monthBind";
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题