Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 48977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:21:52+00:00 2026-05-10T16:21:52+00:00

is there an alternative for mysql_insert_id() php function for PostgreSQL? Most of the frameworks

  • 0

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 of the sequence used in the ID. However, there are times that the primary key is not a serial column….

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T16:21:53+00:00Added an answer on May 10, 2026 at 4:21 pm

    From the PostgreSQL point of view, in pseudo-code:

     * $insert_id = INSERT...RETURNING foo_id;-- only works for PostgreSQL >= 8.2.    * INSERT...; $insert_id = SELECT lastval(); -- works for PostgreSQL >= 8.1   * $insert_id = SELECT nextval('foo_seq'); INSERT INTO table (foo...) values ($insert_id...) for older PostgreSQL (and newer PostgreSQL) 

    pg_last_oid() only works where you have OIDs. OIDs have been off by default since PostgreSQL 8.1.

    So, depending on which PostgreSQL version you have, you should pick one of the above method. Ideally, of course, use a database abstraction library which abstracts away the above. Otherwise, in low level code, it looks like:

    Method one: INSERT… RETURNING

    // yes, we're not using pg_insert() $result = pg_query($db, 'INSERT INTO foo (bar) VALUES (123) RETURNING foo_id'); $insert_row = pg_fetch_row($result); $insert_id = $insert_row[0]; 

    Method two: INSERT; lastval()

    $result = pg_execute($db, 'INSERT INTO foo (bar) values (123);'); $insert_query = pg_query('SELECT lastval();'); $insert_row = pg_fetch_row($insert_query); $insert_id = $insert_row[0]; 

    Method three: nextval(); INSERT

    $insert_query = pg_query($db, 'SELECT nextval('foo_seq');'); $insert_row = pg_fetch_row($insert_query); $insert_id = $insert_row[0]; $result = pg_execute($db, 'INSERT INTO foo (foo_id, bar) VALUES ($insert_id, 123);'); 

    The safest bet would be the third method, but it’s unwieldy. The cleanest is the first, but you’d need to run a recent PostgreSQL. Most db abstraction libraries don’t yet use the first method though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 102k
  • Answers 102k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Found the problem. Since the class stock contains the instance… May 11, 2026 at 8:18 pm
  • Editorial Team
    Editorial Team added an answer Using a comma-separated string for a multi-valued attribute is another… May 11, 2026 at 8:18 pm
  • Editorial Team
    Editorial Team added an answer i think i got it working with this clean routine:… May 11, 2026 at 8:18 pm

Related Questions

I am trying to learn how Hibernate works, and I am running into an
I'm looking for the alternative of mysql_real_escape_string() for SQL Server. Is addslashes() my best
I have an MySQL database which contains some data,it interacts with JSP pages,the thing
How can ALTER be used to drop a column in a MySQL table if

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.