Professional Web Applications Themes

Easy question: how to know the ID of a new record? - PHP Development

Hi, I have the following problem. I am adding a new record in a table. How can I know the ID provided by the database of the new record? suppose my table has 3 attributes: * ID: automatically calculated by the DB server when adding a new record * Val1 * Val2 I am using the Microsoft SQL server DB and the following code: $conn=...... $q="insert into t_mytable values ('value1','value2')"; $result=odbc_exec($conn, $q) or die("".odbc_errormsg()); Now I need to send an email with the ID of this record, but I have an error when I do this: echo odbc_result($result ,"ID"); here ...

  1. #1

    Default Easy question: how to know the ID of a new record?

    Hi,

    I have the following problem.

    I am adding a new record in a table.
    How can I know the ID provided by the database of the new record?

    suppose my table has 3 attributes:
    * ID: automatically calculated by the DB server when adding a new record
    * Val1
    * Val2

    I am using the Microsoft SQL server DB and the following code:
    $conn=......
    $q="insert into t_mytable values ('value1','value2')";
    $result=odbc_exec($conn, $q) or die("".odbc_errormsg());

    Now I need to send an email with the ID of this record, but I have an error
    when I do this:
    echo odbc_result($result ,"ID");
    here is the error:
    Warning: No tuples available at this result index

    So I can't get the ID, even if the new record has successfully been added in
    the db!!
    Actually, I can't get any values of the new field.

    Do you have any idea??
    Is there another way to know the ID of a new record?

    Thanks and best regards



    Rod Guest

  2. #2

    Default Re: Easy question: how to know the ID of a new record?

    "Rod" <totototo.com> wrote:
    > I am adding a new record in a table.
    > How can I know the ID provided by the database of the new record?
    >
    > I am using the Microsoft SQL server DB and the following code:
    > $conn=......
    > $q="insert into t_mytable values ('value1','value2')";
    > $result=odbc_exec($conn, $q) or die("".odbc_errormsg());
    Hi Rod,

    In MS SQL you have to do a select right after the insert in order to get
    the last inserted ID:

    $q="insert into t_mytable values ('value1','value2')";
    $result=odbc_exec($conn, $q) or die("".odbc_errormsg());

    $q2="select identity";
    $result=odbc_exec($conn, $q2) or die("".odbc_errormsg());
    // ....

    Hope this helps;
    JOn
    Jon Kraft Guest

  3. #3

    Default Re: Easy question: how to know the ID of a new record?

    Jon Kraft:
    > "Rod" <totototo.com> wrote:
    >
    >> I am adding a new record in a table.
    >> How can I know the ID provided by the database of the new record?
    >>
    >> I am using the Microsoft SQL server DB and the following code:
    >> $conn=......
    >> $q="insert into t_mytable values ('value1','value2')";
    >> $result=odbc_exec($conn, $q) or die("".odbc_errormsg());
    >
    > Hi Rod,
    >
    > In MS SQL you have to do a select right after the insert in order to get
    > the last inserted ID:
    >
    > $q="insert into t_mytable values ('value1','value2')";
    > $result=odbc_exec($conn, $q) or die("".odbc_errormsg());
    >
    > $q2="select identity";
    > $result=odbc_exec($conn, $q2) or die("".odbc_errormsg());
    > // ....
    I've never used MS SQL or ODBC, but to me it looks like you should do those
    operations within a transaction to ensure their combined atomicity.
    Otherwise you might end up with the wrong id value...

    André Nęss
    André Nęss Guest

  4. #4

    Default Re: Easy question: how to know the ID of a new record?

    On Wed, 09 Jul 2003 20:05:13 +0000, André Nęss
    <andrena.spamreallysifi.uio.no> wrote:
    >Jon Kraft:
    >
    >> "Rod" <totototo.com> wrote:
    >>
    >>> I am adding a new record in a table.
    >>> How can I know the ID provided by the database of the new record?
    >>>
    >>> I am using the Microsoft SQL server DB and the following code:
    >>> $conn=......
    >>> $q="insert into t_mytable values ('value1','value2')";
    >>> $result=odbc_exec($conn, $q) or die("".odbc_errormsg());
    >>
    >> Hi Rod,
    >>
    >> In MS SQL you have to do a select right after the insert in order to get
    >> the last inserted ID:
    >>
    >> $q="insert into t_mytable values ('value1','value2')";
    >> $result=odbc_exec($conn, $q) or die("".odbc_errormsg());
    >>
    >> $q2="select identity";
    >> $result=odbc_exec($conn, $q2) or die("".odbc_errormsg());
    >> // ....
    >
    >I've never used MS SQL or ODBC, but to me it looks like you should do those
    >operations within a transaction to ensure their combined atomicity.
    >Otherwise you might end up with the wrong id value...
    identity is connection-based, so you'll always get "your" inserted
    id value. However, if there is an insert trigger on that table, and it
    inserts new data elsewhere into the database you may get the other
    insterted id instead.

    --
    David (please modify address to david before replying!)
    David Mackenzie Guest

Similar Threads

  1. I have an easy question.
    By six-eric in forum Macromedia Director 3D
    Replies: 1
    Last Post: April 25th, 07:40 PM
  2. Easy question = easy answer?
    By Jyry webforumsuser@macromedia.com in forum Macromedia Freehand
    Replies: 4
    Last Post: September 29th, 03:29 PM
  3. Easy Question ??
    By mark in forum Microsoft Access
    Replies: 1
    Last Post: July 24th, 12:28 PM
  4. ~~EASY QUESTION ~~
    By Adam M. in forum Macromedia Dreamweaver
    Replies: 3
    Last Post: July 16th, 05:01 PM
  5. Easy Question/Easy Answer
    By ChuckyJ webforumsuser@macromedia.com in forum Macromedia Dreamweaver
    Replies: 1
    Last Post: July 12th, 01:50 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