473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

Rod
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

Jul 16 '05 #1
3 9365
"Rod" <to**@toto.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
Jul 16 '05 #2
Jon Kraft:
"Rod" <to**@toto.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
Jul 16 '05 #3
On Wed, 09 Jul 2003 20:05:13 +0000, André Nęss
<an*********************@ifi.uio.no> wrote:
Jon Kraft:
"Rod" <to**@toto.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!)
Jul 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: nospam_stunter2001 | last post by:
Hi folks, I'm new here and am a neophyte Access 2000 user/developer. A year ago, I barely knew what Access was. I was asked by the people that pay us to begin keeping certain data about the...
20
by: Jack Schitt | last post by:
I thought I was starting to get a handle on Access, until I tried doing something useful...now I'm stuck. I have a DB with two tables - to keep it simple I'll say that one is an Employee File...
36
by: No Spam | last post by:
Dear fellow Access 2003 Users, Is there a way to trim all of the fields in a table in one swoop using VBA (preferred) or a query? Right now, I am using an update query and updating EACH field...
5
by: LedZep | last post by:
What up, All I need to do is enter a last name in a text box, query a MSAccess database and display the name with the corresponding columns. This is no problem, but when there are more than one...
29
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this...
14
by: google | last post by:
I am creating a new database for use within our company, that I'd like to make reasonably secure (short of a true server based solution). The back-end of a non-server based database seems to be...
1
by: Ryker | last post by:
I have a lengthy form containing several fields of data. Each record that is entered is given a record identification number. Sometimes the user gets midway into the form and has to stop to...
4
by: n | last post by:
Hello! Here is a problem I hope you can point me to a solution. It Problem: A teacher needs to know which lesson to teach. A school has a curriculum with 26 lessons, A-Z. For a given class,...
1
by: =?Utf-8?B?Qi4gQ2hlcm5pY2s=?= | last post by:
(If I'm overlooking anything, please let me know.) First, my only concern is updating single records in a Detailsview using an ObjectDataSource. The target table has a timestamp field. Assume ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.