SELECT CURRVAL(pg_get_serial_sequence我做错了什么?

时间:2013-07-26 08:42:56

标签: postgresql psql postgresql-9.3

我做错了什么,我试图从表中获取最后一个record_id,在第一个语句将其插入到我的表中之后。我似乎只打印了用于显示最后一个ID的代码?

      SELECT CURRVAL (pg_get_serial_sequence('sheet_tbl','sheet_id'))";

此处代码

  else {
        echo 'Record added';
        $sql = "INSERT INTO sheet_tbl (site_id,  eventdate, eventtime, username, additionalvolunteers) VALUES ('$_POST[site_id]','$_POST[eventdate]','$_POST[eventtime]', '$username','$_POST[additionalvolunteers]')";

        echo $sql; //Just so I can see what is getting sent
        $result = pg_query($sql);

        $sheet_id_pull = "SELECT CURRVAL (pg_get_serial_sequence('sheet_tbl','sheet_id'))";
        echo $sheet_id_pull; //This is where im having the issue with the above line.
}

1 个答案:

答案 0 :(得分:2)

也许

echo pg_query($sheet_id_pull);

而不是

echo $sheet_id_pull;

$sheet_id_pull = pg_query("SELECT CURRVAL (pg_get_serial_sequence('sheet_tbl','sheet_id'))");
echo $sheet_id_pull;

另请阅读this问题。它有一个更好的方法来获取插入的id。