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

I Wrote a sql code in yii and its giving this error:

CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1. The SQL statement executed was: SELECT * FROM users where id in ()

I had no idea why this is happening......... The code is:

$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>";

When I echo the sql using echo $sql2 and die() to see the sql, it gives me this: SELECT * FROM users where id in (44,45)

Now, I used above sql directly in the the as

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

and its work perfectly, I do not know what to do with my sql.


You got error when $array12 is empty:

SELECT * FROM users where id in () (check whole sql at the end of a error's message)

You have to check to count elements in $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/60850.html

上一篇: 如何使用yii frameowrk在SQL Query中提供单引号?

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