我有代码
$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"
答案 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"];
这很简单