yii queryAll failes传递参数

时间:2014-03-16 16:29:07

标签: php mysql yii dao

我正在路过:

Yii::app()->db->createCommand($query)->queryAll(true,array(':bizid'=>$params));

查询:

 SELECT DISTINCT(BI.item_name)
 FROM items BI 
 WHERE BI.b_id IN (:bizid) ORDER BY BI.item_name ASC 

查询很容易找到结果,但此查询会得到结果:

SELECT DISTINCT(BI.item_name) as i_name FROM blacklist_items BI WHERE BI.business_id IN   (165,664,842,866,1234,3007,3030,3031,3032) ORDER BY BI.item_name ASC

为什么queryAll会失败?

2 个答案:

答案 0 :(得分:0)

将ID分配给数组:

$ids = [165,664,842,866,1234,3007,3030,3031,3032];

查询应为:

$query = "SELECT DISTINCT(BI.item_name)"
 . " FROM items BI"
 . " WHERE BI.b_id IN (" . implode(',', $ids) . ") ORDER BY BI.item_name ASC 

答案 1 :(得分:0)

在我的项目中遇到过这个问题几次我已经提出了以下Yii使用CDbCriteria来解决这个问题,这个问题很简单,但它提供了param计数匹配的安全性。

当应用于您的示例时,我的代码将是:

$business_ids = array(1,2,3,4,5);
$criteria = new CDbCriteria();
$criteria->addInCondition('business_id',$business_ids);

$sql = 'SELECT DISTINCT(item_name) 
          FROM items 
         WHERE '.$criteria->condition.'
      ORDER BY item_name ASC';

$command = Yii::app()->db->createCommand($sql);
$results = $command->queryColumn($criteria->params);