我有以下代码序列:
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似乎是空的,我不知道为什么。
答案 0 :(得分:1)
问题是绑定PDO参数的问题,但我现在无法调试它。我可以为您提供更优雅的变体工作解决方案(使用Query Builder):
$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']
);
}
答案 1 :(得分:0)
DO queryAll():
$dataReader = $command->queryAll();
答案 2 :(得分:0)
首先 foreach 循环
$in_elems[] = ":team_$key";
没有单逗号字符。