PDO postgresql最后插入id返回' false'或-1

时间:2017-11-03 13:40:08

标签: postgresql pdo lastinsertid

我有代码

$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为空。

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

并收到一个值" -1"

1 个答案:

答案 0 :(得分:0)

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

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

这很简单