mysql_insert_id alternative for postgresql

后端 未结 4 529
借酒劲吻你
借酒劲吻你 2020-12-01 15:53

is there an alternative for mysql_insert_id() php function for PostgreSQL? Most of the frameworks are solving the problem partially by finding the current value

4条回答
  •  旧时难觅i
    2020-12-01 16:36

    From php.net:

    $res=pg_query("SELECT nextval('foo_key_seq') as key");
    $row=pg_fetch_array($res, 0);
    $key=$row['key'];
    // now we have the serial value in $key, let's do the insert
    pg_query("INSERT INTO foo (key, foo) VALUES ($key, 'blah blah')");
    

    This should always provide unique key, because key retrieved from database will be never retrieved again.

提交回复
热议问题