Professional Web Applications Themes

PHP-MySQL: Returning ID when new record is made? - PHP Development

Let's say I create a new record in a table like this: mysql_query("INSERT INTO table (col1) VALUES ('example')",$conn); ....that had an auto-incrementing, unique identifying column named "ID" that would be populated with a unique number upon insertion... How can I get the unique number it assigned to that record returned for futher use? -- [ Sugapablo ] [ [url]http://www.sugapablo.com[/url] <--music ] [ [url]http://www.sugapablo.net[/url] <--personal ] [ [email]sugapablo12jabber.com[/email] <--jabber IM ]...

  1. #1

    Default PHP-MySQL: Returning ID when new record is made?

    Let's say I create a new record in a table like this:
    mysql_query("INSERT INTO table (col1) VALUES ('example')",$conn);

    ....that had an auto-incrementing, unique identifying column named "ID"
    that would be populated with a unique number upon insertion...

    How can I get the unique number it assigned to that record returned for
    futher use?


    --
    [ Sugapablo ]
    [ [url]http://www.sugapablo.com[/url] <--music ]
    [ [url]http://www.sugapablo.net[/url] <--personal ]
    [ [email]sugapablo12jabber.com[/email] <--jabber IM ]
    Sugapablo Guest

  2. #2

    Default Re: PHP-MySQL: Returning ID when new record is made?

    On Wed, 26 Nov 2003 18:47:46 -0000, Sugapablo <russREMOVEsugapablo.com> wrote:
    >Let's say I create a new record in a table like this:
    >mysql_query("INSERT INTO table (col1) VALUES ('example')",$conn);
    >
    >...that had an auto-incrementing, unique identifying column named "ID"
    >that would be populated with a unique number upon insertion...
    >
    >How can I get the unique number it assigned to that record returned for
    >futher use?
    [url]http://uk2.php.net/manual/en/ref.mysql.php[/url]

    --
    Andy Hassall (andyandyh.co.uk) icq(5747695) ([url]http://www.andyh.co.uk[/url])
    Space: disk usage ysis tool ([url]http://www.andyhsoftware.co.uk/space[/url])
    Andy Hassall Guest

  3. #3

    Default Re: PHP-MySQL: Returning ID when new record is made?

    Sugapablo wrote:
    > Let's say I create a new record in a table like this:
    > mysql_query("INSERT INTO table (col1) VALUES ('example')",$conn);
    >
    > ...that had an auto-incrementing, unique identifying column named "ID"
    > that would be populated with a unique number upon insertion...
    >
    > How can I get the unique number it assigned to that record returned for
    > futher use?
    Check out your php manual ( [url]http://www.php.net/[/url] ) for this function:

    int mysql_insert_id ( [resource link_identifier])

    Aggro Guest

  4. #4

    Default Re: PHP-MySQL: Returning ID when new record is made?

    select last_insert_id()

    after the insert..

    But you can read the manual which is very available and it will tell you
    this.

    Thanks

    Aaron

    "Sugapablo" <russREMOVEsugapablo.com> wrote in message
    news:slrnbs9taa.nru.russREMOVEdell.sugapablo.net. ..
    > Let's say I create a new record in a table like this:
    > mysql_query("INSERT INTO table (col1) VALUES ('example')",$conn);
    >
    > ...that had an auto-incrementing, unique identifying column named "ID"
    > that would be populated with a unique number upon insertion...
    >
    > How can I get the unique number it assigned to that record returned for
    > futher use?
    >
    >
    > --
    > [ Sugapablo ]
    > [ [url]http://www.sugapablo.com[/url] <--music ]
    > [ [url]http://www.sugapablo.net[/url] <--personal ]
    > [ [email]sugapablo12jabber.com[/email] <--jabber IM ]

    Aaron Miles Guest

  5. #5

    Default Re: PHP-MySQL: Returning ID when new record is made?


    Read here:

    [url]http://www.mysql.com/doc/en/Miscellaneous_functions.html[/url]

    Search for "LAST_INSERT_ID"

    -John



    Sugapablo wrote:
    > Let's say I create a new record in a table like this:
    > mysql_query("INSERT INTO table (col1) VALUES ('example')",$conn);
    >
    > ...that had an auto-incrementing, unique identifying column named "ID"
    > that would be populated with a unique number upon insertion...
    >
    > How can I get the unique number it assigned to that record returned for
    > futher use?
    >
    >
    John Guest

  6. #6

    Default Re: PHP-MySQL: Returning ID when new record is made?

    Sugapablo wrote:
    > Let's say I create a new record in a table like this:
    > mysql_query("INSERT INTO table (col1) VALUES ('example')",$conn);
    >
    > ...that had an auto-incrementing, unique identifying column named "ID"
    > that would be populated with a unique number upon insertion...
    >
    > How can I get the unique number it assigned to that record returned for
    > futher use?
    >
    >
    mysql_insert_id()

    [url]http://us2.php.net/manual/en/function.mysql-insert-id.php[/url]

    Bruce Wolk Guest

  7. #7

    Default Re: PHP-MySQL: Returning ID when new record is made?

    There is function in PHP mysql_insert_id(), which return what you need.

    "Sugapablo" <russREMOVEsugapablo.com> wrote in message
    news:slrnbs9taa.nru.russREMOVEdell.sugapablo.net. ..
    > Let's say I create a new record in a table like this:
    > mysql_query("INSERT INTO table (col1) VALUES ('example')",$conn);
    >
    > ...that had an auto-incrementing, unique identifying column named "ID"
    > that would be populated with a unique number upon insertion...
    >
    > How can I get the unique number it assigned to that record returned for
    > futher use?
    >
    >
    > --
    > [ Sugapablo ]
    > [ [url]http://www.sugapablo.com[/url] <--music ]
    > [ [url]http://www.sugapablo.net[/url] <--personal ]
    > [ [email]sugapablo12jabber.com[/email] <--jabber IM ]

    DimaT Guest

  8. #8

    Default Re: PHP-MySQL: Returning ID when new record is made?

    Bruce Wolk wrote:
    > Sugapablo wrote:
    >
    >> Let's say I create a new record in a table like this:
    >> mysql_query("INSERT INTO table (col1) VALUES ('example')",$conn);
    >>
    >> ...that had an auto-incrementing, unique identifying column named "ID"
    >> that would be populated with a unique number upon insertion...
    >>
    >> How can I get the unique number it assigned to that record returned for
    >> futher use?
    >>
    >>
    >
    > mysql_insert_id()
    >
    > [url]http://us2.php.net/manual/en/function.mysql-insert-id.php[/url]
    >
    Does mysql_insert_id() return the last record inserted by ANYONE, or the
    last record inserted by the current instance?

    In other words, suppose there's a site with heavy traffic that
    manipulates a database. Two users get on and insert info into the db at
    about the same time.

    user1 inserts data
    user2 inserts data
    user1 mysql_insert_id()
    user2 mysql_insert_id()

    will user1 get the id of the data he *just* inserted, or will he get
    user2's id?

    --
    Jim Thomas Principal Applications Engineer Bittware, Inc
    [email]jthomasbittware.com[/email] [url]http://www.bittware.com[/url] (703) 779-7770
    When you have a new hammer, the whole world looks like a nail.

    Jim Thomas Guest

  9. #9

    Default Re: PHP-MySQL: Returning ID when new record is made?


    On 5-Dec-2003, Jim Thomas <jthomasbittware.com> wrote:
    > Bruce Wolk wrote:
    > > Sugapablo wrote:
    > >
    > >> Let's say I create a new record in a table like this:
    > >> mysql_query("INSERT INTO table (col1) VALUES ('example')",$conn);
    > >>
    > >> ...that had an auto-incrementing, unique identifying column named "ID"
    > >> that would be populated with a unique number upon insertion...
    > >>
    > >> How can I get the unique number it assigned to that record returned for
    > >> futher use?
    > >>
    > >>
    > >
    > > mysql_insert_id()
    > >
    > > [url]http://us2.php.net/manual/en/function.mysql-insert-id.php[/url]
    > >
    >
    > Does mysql_insert_id() return the last record inserted by ANYONE, or the
    > last record inserted by the current instance?
    It returns the insert id of the last insert for a particular link_id ($conn
    in the above example). It would be pretty useless otherwise.

    The doentation is pretty clear on this.

    --
    Tom Thackrey
    [url]www.creative-light.com[/url]
    tom (at) creative (dash) light (dot) com
    do NOT send email to [email]jamesbutlerwillglen.net[/email] (it's reserved for spammers)
    Tom Thackrey Guest

Similar Threads

  1. returning an inserted SQL record
    By JoeyTMann in forum Coldfusion Database Access
    Replies: 2
    Last Post: March 22nd, 04:38 PM
  2. Functions returning RECORD
    By Craig Bryden in forum PostgreSQL / PGSQL
    Replies: 4
    Last Post: January 14th, 01:37 PM
  3. FileIndex MSIDXS error returning record
    By Tim in forum ASP.NET General
    Replies: 0
    Last Post: July 18th, 02:16 PM
  4. Adding record to a MYSQL DB using PHP ?
    By James in forum PHP Development
    Replies: 1
    Last Post: July 9th, 12:00 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139