YII中的SQL错误::: SQLSTATE [42000]:语法错误或访问冲突:1064

我在yii中写了一个sql代码,并给出了这个错误:

CDbCommand无法执行SQL语句:SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法错误; 检查与您的MySQL服务器版本相对应的手册,以在第1行')'附近使用正确的语法。执行的SQL语句为:SELECT * FROM users where id in()

我不知道为什么会发生这种情况.........代码是:

$t = implode(",", $array12);
echo $t;
$sql2    = 'SELECT * FROM users where id in ('. $t. ')';
// echo $sql2; die;
$command = $connection->createCommand($sql2);
$row5    = $command->queryAll();

echo "<pre>";
print_r($row5);
echo "</pre>";

当我使用echo $ sql2和die()来查看sql时,它会给我这个:SELECT * FROM users where in(44,45)

现在,我直接在as上面使用sql

$sql2    = 'SELECT * FROM users where id in (44,45)';
$command = $connection->createCommand($sql2);
$row5    = $command->queryAll();

和它的工作完美,我不知道如何处理我的SQL。


$ array12为空时发生错误:

SELECT * FROM用户id in()(检查错误消息结尾的整个sql)

你必须检查$ array12中的元素:

if (count($array12)) {
  $t = implode(",", $array12);
  $sql2    = 'SELECT * FROM users where id in ('. $t. ')';
  // echo $sql2; die;
  $command = $connection->createCommand($sql2);
  $row5    = $command->queryAll();
} else {
  $row5    = array();
}
链接地址: http://www.djcxy.com/p/60849.html

上一篇: SQL error in YII ::: SQLSTATE[42000]: Syntax error or access violation: 1064

下一篇: Showing data from 2 models in a grid with Yii