-1

我有以下代码序列:

if (is_array($team) && !empty($team)) {
    $where_clause = "WHERE te.name IN (";
    $in_elems = array();
    foreach ($team as $key => $value) {
        $in_elems[] = "':team_$key'";
    }
    $where_clause .= implode(',', $in_elems) . " )";
    $query = "SELECT p.name FROM " . Yii::app()->params['team_tbl'] . " te  
         LEFT JOIN " . Yii::app()->params['player_tbl'] . " p ON te.id = p.team_id
         $where_clause 
         ORDER BY te.name, p.name ASC
    ";
    $command = $conn->createCommand($query);
    foreach ($team as $key => $value) {
        $command->bindValue(":team_$key", $value);
    }
} else {
    ....
}
if ($command) {
    $dataReader = $command->query();
    print_R($dataReader->count()); ---> here is 0!!!
    while(($player = $dataReader->read()) !== false) {
        $players[] = array(
            'label'=>$player['name'],
            'id'=>$player['name'],
    );
}

当我运行这个代码序列时,dataReader 似乎是空的,我不知道为什么。

4

3 回答 3

1

问题在于绑定 PDO 参数,但我目前无法调试它。我可以为您的变体提供更优雅的工作解决方案(使用查询生成器):

$models = Yii::app()->db->createCommand()
    ->select('p.name')
    ->from('team te')
    ->join('player p', 'te.id=p.team_id')
    ->where(array('in', 'te.id', $team))
    ->order('te.name, p.name ASC')
    ->queryAll();

$players = array();
foreach ($models as $model) {
    $players[] = array(
        'label' => $model['name'],
        'id' => $model['name']
    );
}
于 2013-10-21T20:36:45.987 回答
0

做查询所有():

$dataReader = $command->queryAll();
于 2013-10-20T11:33:46.050 回答
0

第一个foreach循环

$in_elems[] = ":team_$key";

没有单逗号字符。

于 2013-10-21T16:13:23.673 回答