我刚刚在 Controller 中执行了一些代码,并试图找到最后插入的 id。
但是显示错误:
这是我的代码:
$sql = 'INSERT into "Tbl_Community" ("User_id","Community_name") VALUES (10,'new community')';
$connection = Yii::app() -> db;
$command = $connection -> createCommand($sql);
$command -> execute();
echo $connection->getLastInsertID();
错误:
[message:protected] => SQLSTATE[42602]: Invalid name: 7 ERROR: invalid name syntax
[string:Exception:private] =>
[code:protected] => 42602
[file:protected] => D:\wamp\www\Tiein\framework\db\CDbConnection.php
[line:protected] => 548
[trace:Exception:private] => Array
(
[0] => Array
(
[file] => D:\wamp\www\Tiein\framework\db\CDbConnection.php
[line] => 548
[function] => lastInsertId
[class] => PDO
[type] => ->
[args] => Array
(
[0] =>
)
)
最佳答案
使用 postgresql 你不能用这种方式排在最后
$lastId= Yii::app()->db->getLastInsertID();
试试这个(tbl_user_group_id 是你的表 id 列,_seq 是用于 postgresql 的)
$lastId = Yii::app()->db->getLastInsertID('tbl_user_group_id_seq');
或
$lastId= Yii::app()->db->getCommandBuilder()->getLastInsertID('{{tablename}}') ;
关于postgresql - Yii 中最后一次插入 id 返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22042761/