【问题标题】:What conditions are necessary for lastInsertId to work [duplicate]lastInsertId 工作需要哪些条件[重复]
【发布时间】:2013-11-13 16:38:38
【问题描述】:

我正试图围绕 PDO。 $lastid 怎么没有输出?

function renderRoot($db){
    $sql = "INSERT INTO nodes (name) VALUES ('/');";
    $response = $db->query($sql);
    $lastid = $db->lastInsertId();
    echo $lastid;
    return;
}

代码向表中添加了一个值,并且它有一个名为 id 的列,该列是递增的。

这是我的 sql(postgresql):

$nodetable = "create table nodes (
    id serial primary key,
    parentid integer references nodes(id ),
    name varchar
);";

【问题讨论】:

  • 表格是否有自增列?插入成功了吗?
  • 插入成功。我会用 sql 更新问题。
  • 请改用INSERT ... RETURNING ...lastInsertId 这个东西是仿照 mysql 而不是 postgres 的最佳选择。

标签: postgresql pdo


【解决方案1】:

在 postgresql 中,lastInsertId() 接受一个参数,在你的情况下,是序列的名称,即 nodes_id_seq

$lastid = $db->lastInsertId('nodes_id_seq');

否则它将返回最后一个 oid,如果有的话。

【讨论】:

  • 值得注意的是,节点是我的数据库,id 是列。谢谢!
猜你喜欢
  • 1970-01-01
  • 2019-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-25
  • 2011-03-17
  • 1970-01-01
相关资源
最近更新 更多