【问题标题】:PDO postgresql last inserted id return 'false' or -1PDO postgresql 最后插入的 id 返回 'false' 或 -1
【发布时间】:2017-11-03 13:40:07
【问题描述】:

我有代码

$dbh = new PDO("pgsql:host=localhost; port=5432; dbname=gpd; user=postgres; password=postgres");
$sth = $dbh->prepare("INSERT INTO res (res_name, res_order) VALUES ('London', 1)");
$sth->execute();
$id = $dbh->lastInsertId();
echo $id;

记录插入,但 $id 为空。

我尝试执行查询 INSERT INTO res (res_name, res_order) VALUES ('London', 1) RETURNING id

并收到一个值“-1”

【问题讨论】:

  • 可以分享res的表结构吗?
  • CREATE TABLE res (id SERIAL PRIMARY KEY, res_name VARCHAR(100) DEFAULT '' NOT NULL, res_order INT DEFAULT 1 NOT NULL)

标签: postgresql pdo lastinsertid


【解决方案1】:

回家,我自己明白必须做什么

$dbh = new PDO("pgsql:host=localhost; port=5432; dbname=gpd; user=postgres; password=postgres");    
$sth = $dbh->prepare("INSERT INTO res (res_name, res_order) VALUES ('London', 1)");    
$sth->execute();    
$result = $sth->fetch(PDO::FETCH_ASSOC);    
echo "ID - ". $result["id"];

很简单

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 2012-01-15
    • 2012-05-31
    • 1970-01-01
    • 2016-04-16
    • 2013-05-26
    • 1970-01-01
    相关资源
    最近更新 更多