Return an primary key value after insert query?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Julian  Moorhouse

    Return an primary key value after insert query?

    Hi,

    I'm fairly new to mysql, but I have a reasonable knowledge of PHP.

    I just want to know is theres a simple way to return a primary key
    value for a record I've just inserted?

    In other environments I'd set a flag using the current time stamp which
    is inserted with the record.

    Then I'd select on that timestamp and get the primary key value.

    Is there a simpler way?

    Jules.

  • David Haynes

    #2
    Re: Return an primary key value after insert query?

    Julian Moorhouse wrote:
    Hi,
    >
    I'm fairly new to mysql, but I have a reasonable knowledge of PHP.
    >
    I just want to know is theres a simple way to return a primary key
    value for a record I've just inserted?
    >
    In other environments I'd set a flag using the current time stamp which
    is inserted with the record.
    >
    Then I'd select on that timestamp and get the primary key value.
    >
    Is there a simpler way?
    >
    Jules.
    >
    If your primary key is made using AUTO_INCREMENT, you may use the
    mysqli_insert_i d function.

    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


    -david-

    Comment

    • Julian  Moorhouse

      #3
      Re: Return an primary key value after insert query?

      Hi,

      Thanks for your reply.

      I don't think I have mysqli on my ISPs server.

      Jules.

      Comment

      • Rik

        #4
        Re: Return an primary key value after insert query?

        Julian Moorhouse wrote:
        Hi,
        >
        Thanks for your reply.
        >
        I don't think I have mysqli on my ISPs server.
        normal mysql_insert_id () works too.

        Tip: if you're new to something in PHP, getting the applicable chapter from
        the function reference is very handy. In this case,
        http://www.php.net/manual/en/ref.mysql.php, which lists all functions
        associated with mysql.

        Grtz,
        --
        Rik Wasmus


        Comment

        • Julian  Moorhouse

          #5
          Re: Return an primary key value after insert query?

          Ah..

          echo mysql_insert_id ();

          works too

          Thanks,

          Jules.

          Comment

          • Kimmo Laine

            #6
            Re: Return an primary key value after insert query?

            "Julian Moorhouse" <mindwarpltd@gm ail.comwrote in message
            news:1152613948 .020421.139900@ m73g2000cwd.goo glegroups.com.. .
            Hi,
            >
            Thanks for your reply.
            >
            I don't think I have mysqli on my ISPs server.

            Same difference. The same fucntion exists also for "plain" mysql, just
            remove the extra "i". Ie. use mysql_insert_id ().

            --
            "ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
            spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)


            Comment

            • Adam

              #7
              Re: Return an primary key value after insert query?

              On Tue, 11 Jul 2006 12:39:11 +0200, Rik wrote:
              >Julian Moorhouse wrote:
              >Hi,
              >>
              >Thanks for your reply.
              >>
              >I don't think I have mysqli on my ISPs server.
              >
              >normal mysql_insert_id () works too.
              >
              >Tip: if you're new to something in PHP, getting the applicable chapter from
              >the function reference is very handy. In this case,
              >http://www.php.net/manual/en/ref.mysql.php, which lists all functions
              >associated with mysql.
              .... *and* it has loads of really helpful hints & snippets about each
              function. A veritable gold mine!

              Adam.

              Comment

              Working...