doudi4014 2014-06-12 20:59
浏览 36
已采纳

PDO说我缺少令牌,但我不是 - 这有什么不对[关闭]

So I have managed to spit out (through var_dump) the following:

// This is my whole select statement
string 'SELECT * FROM logs AS LOG WHERE ( LOG.DATE BETWEEN :startMonth AND :endMonth ) AND LOG.VALUE LIKE :filter ' (length=106)

// This is the database object (note the params)
object(Micro\Database)[1667]
  protected 'query' => string 'SELECT * FROM logs AS LOG WHERE ( LOG.DATE BETWEEN :startMonth AND :endMonth ) ' (length=79)
  protected 'params' => 
    array
      ':startMonth' => 
        object(DateTime)[1668]
          public 'date' => string '2014-06-01 00:00:00' (length=19)
          public 'timezone_type' => int 3
          public 'timezone' => string 'UTC' (length=3)
      ':endMonth' => 
        object(DateTime)[1671]
          public 'date' => string '2014-06-30 23:59:59' (length=19)
          public 'timezone_type' => int 3
          public 'timezone' => string 'UTC' (length=3)
      ':filter' => string '%"unit_id": 1030006431%' (length=23)

So in the query we see that I am using :startMonth, :endMonth and :filter and then in the params We see I am setting the same variables. everything should be there. But apparently it's not:

'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens'

Uh .... no all the tokens are there. Let look at the code

public static function findByWildCard($unitId=null, $messageType=null, $filter=null, $month) {
    $sqlSelect = 'SELECT * ' .
                    'FROM logs AS LOG '.
                    'WHERE ( LOG.DATE BETWEEN :startMonth AND :endMonth ) ';


    // Connect to lazarus
    $query = self::connectToLazarus();

    // Start of the specified month. Eg. 2013-01-01 00:00:00
    $startMonth = \DateTime::createFromFormat('Y-m-d H:i:s', $month.' 00:00:00');

    // End of the specified month. Eg. 2013-01-31 11:59:59
    $endMonth = clone $startMonth;
    $oneMonth = \DateInterval::createFromDateString('1 month');
    $oneSecond = \DateInterval::createFromDateString('1 second');
    $endMonth->add($oneMonth)->sub($oneSecond);

    // Get the results from the query
    $query->setQuery($sqlSelect)
        ->setParameter('startMonth', $startMonth)
        ->setParameter('endMonth', $endMonth);

    if (!empty($filter)) {
        $sqlSelect .= 'AND LOG.VALUE LIKE :filter ';
        $query->setParameter('filter', '%'.$filter.'%');
    }

    var_dump($query->getResult()); exit;

}

What is going on?

  • 写回答

2条回答 默认 最新

  • duanhuang1699 2014-06-12 21:07
    关注

    You are setting the query before you add the filter to the query string.

    // Get the results from the query
    $query->setQuery($sqlSelect)
        ->setParameter('startMonth', $startMonth)
        ->setParameter('endMonth', $endMonth);
    
    if (!empty($filter)) {
        $sqlSelect .= 'AND LOG.VALUE LIKE :filter ';
        $query->setParameter('filter', '%'.$filter.'%');
    }
    

    You are setting the query to $sqlSelect and aftwards appending the filter part after setting the query but never setting the query again to the new value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?