lastInsertId 在 PostgreSQL 中总是返回 FALSE
lastInsertId always returning FALSE in PostgreSQL
我正在尝试插入一行并将其主键作为输出。但是,当我使用 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";
手册指出:
Returns the ID of the last inserted row, or the last value from a sequence object, depending on the underlying driver. For example, PDO_PGSQL requires you to specify the name of a sequence object for the name parameter.
您需要传入序列对象作为Postgresql的参数。
我正在尝试插入一行并将其主键作为输出。但是,当我使用 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";
手册指出:
Returns the ID of the last inserted row, or the last value from a sequence object, depending on the underlying driver. For example, PDO_PGSQL requires you to specify the name of a sequence object for the name parameter.
您需要传入序列对象作为Postgresql的参数。