lastInsertId在PostgreSQL中总是返回FALSE



我正试图插入一行并获得其主键作为输出。然而,当我使用lastInsertId()时,我总是得到False作为输出。

下面是代码片段。你能告诉我我哪里做错了吗?

$host        = "host=localhost";
$port        = "port=5432";
$dbname      = "dbname=TestDB";
$credentials = "user=Admin password=Admin";
// Connecting, selecting database
// $dbconn = pg_connect("$host $port $dbname $credentials") or die('Could not connect: ' . pg_last_error());
$conn = new PDO("pgsql:dbname=webdev;host=localhost;port=5432", 'postgres' , 'rameshnr');
// $userName = $_GET['userName'];
// $userComment = $_GET['userComment'];
$userID = 'B101';
$parentComment = 0;
$commentText = 'Test Comment';
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("INSERT INTO table_Comments(userID, parentComment, commentText) VALUES (:userID, :parentComment, :commentText)");
$stmt->bindParam(':userID', $userID, PDO::PARAM_STR, 32);
$stmt->bindParam(':parentComment', $parentComment, PDO::PARAM_INT);
$stmt->bindParam(':commentText', $commentText, PDO::PARAM_STR, 500);    

$stmt->execute();
$result= $conn->lastInsertId();
echo "$result";

手册说明:

返回最后插入行的ID,或序列对象的最后一个值,具体取决于底层驱动程序。例如,PDO_PGSQL要求您为name参数指定序列对象的名称。

你需要传递序列对象作为Postgresql的参数

最新更新