donglianglu8136 2015-05-18 03:34
浏览 103
已采纳

lastInsertId总是在PostgreSQL中返回FALSE [复制]

This question already has an answer here:

I am trying to insert a Row and get its Primary Key as the Output. However, when I use lastInsertId(), I am always getting False as Output.

Below is the code Snippet. Could you please let me know where I am going Wrong ?

$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";
</div>
  • 写回答

1条回答 默认 最新

  • dongmu6225 2015-05-18 03:37
    关注

    The manual states:

    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.

    You need to pass in the sequence object as the parameter for Postgresql.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集