简体   繁体   中英

Query error in 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();
 }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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