473,386 Members | 1,674 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,386 software developers and data experts.

Finding primary key of record after INSERT

Hi,

I have a mysql db with with autoincrementing primary keys in each table. I
want to allow a user to add a record using a php webpage. Once the page is
added how can I re SELECT that record (for the user to verify) if I don't
know what the primary key was when it was inserted?

Is the only way to not use autoincrement and choose the primary key value
myself?

Thanks

--
Gabriel Harrison
1 Hawthorn Crescent
Durham
DH1 1ED
Telephone: 0191 370 9135
Mobile: 07939 231 720
Jul 19 '05 #1
6 16704
Gabriel - if your connection to your mysql db is persistent - you should be
able to call the
LAST_INSERT_ID
function ..
and it will return the PK of the row just inserted.
see here for more info:
http://www.mysql.com/doc/en/Miscella...functions.html

hth - mondo regards [Bill]
--
William Sanders / Electronic Filing Group Remove the DOT BOB to reply via
email.
FREE LONG DISTANCE -> mailto:ex********@efgroup.net
mySql / VFP / MS-SQL
"Gabriel Harrison" <Ga*****@gabrielharrison.co.uk> wrote in message
news:bj**********@news6.svr.pol.co.uk...
Hi,

I have a mysql db with with autoincrementing primary keys in each table. I
want to allow a user to add a record using a php webpage. Once the page is
added how can I re SELECT that record (for the user to verify) if I don't
know what the primary key was when it was inserted?

Is the only way to not use autoincrement and choose the primary key value
myself?

Thanks

--
Gabriel Harrison
1 Hawthorn Crescent
Durham
DH1 1ED
Telephone: 0191 370 9135
Mobile: 07939 231 720

Jul 19 '05 #2
Gabriel - if your connection to your mysql db is persistent - you should be
able to call the
LAST_INSERT_ID
function ..
and it will return the PK of the row just inserted.
see here for more info:
http://www.mysql.com/doc/en/Miscella...functions.html

hth - mondo regards [Bill]
--
William Sanders / Electronic Filing Group Remove the DOT BOB to reply via
email.
FREE LONG DISTANCE -> mailto:ex********@efgroup.net
mySql / VFP / MS-SQL
"Gabriel Harrison" <Ga*****@gabrielharrison.co.uk> wrote in message
news:bj**********@news6.svr.pol.co.uk...
Hi,

I have a mysql db with with autoincrementing primary keys in each table. I
want to allow a user to add a record using a php webpage. Once the page is
added how can I re SELECT that record (for the user to verify) if I don't
know what the primary key was when it was inserted?

Is the only way to not use autoincrement and choose the primary key value
myself?

Thanks

--
Gabriel Harrison
1 Hawthorn Crescent
Durham
DH1 1ED
Telephone: 0191 370 9135
Mobile: 07939 231 720

Jul 19 '05 #3
Gabriel - if your connection to your mysql db is persistent - you should be
able to call the
LAST_INSERT_ID
function ..
and it will return the PK of the row just inserted.
see here for more info:
http://www.mysql.com/doc/en/Miscella...functions.html

hth - mondo regards [Bill]
--
William Sanders / Electronic Filing Group Remove the DOT BOB to reply via
email.
FREE LONG DISTANCE -> mailto:ex********@efgroup.net
mySql / VFP / MS-SQL
"Gabriel Harrison" <Ga*****@gabrielharrison.co.uk> wrote in message
news:bj**********@news6.svr.pol.co.uk...
Hi,

I have a mysql db with with autoincrementing primary keys in each table. I
want to allow a user to add a record using a php webpage. Once the page is
added how can I re SELECT that record (for the user to verify) if I don't
know what the primary key was when it was inserted?

Is the only way to not use autoincrement and choose the primary key value
myself?

Thanks

--
Gabriel Harrison
1 Hawthorn Crescent
Durham
DH1 1ED
Telephone: 0191 370 9135
Mobile: 07939 231 720

Jul 19 '05 #4
Ray
"Gabriel Harrison" <Ga*****@gabrielharrison.co.uk> wrote in message news:<bj**********@news6.svr.pol.co.uk>...
Hi,

I have a mysql db with with autoincrementing primary keys in each table. I
want to allow a user to add a record using a php webpage. Once the page is
added how can I re SELECT that record (for the user to verify) if I don't
know what the primary key was when it was inserted?

Is the only way to not use autoincrement and choose the primary key value
myself?

Thanks


There is a LAST_INSERT_ID function that does this, though I don't use
it as apparently you can point it to a specific table (at least in the
documentation examples). So I do this by doing something like:
SELECT MAX(ID) AS ID FROM table WHERE addedby = 'me'
with the WHERE clause filtering on a field value that was just
inserted so that I can be sure it is looking at only the records that
I added myself.
Jul 19 '05 #5
Ray
"Gabriel Harrison" <Ga*****@gabrielharrison.co.uk> wrote in message news:<bj**********@news6.svr.pol.co.uk>...
Hi,

I have a mysql db with with autoincrementing primary keys in each table. I
want to allow a user to add a record using a php webpage. Once the page is
added how can I re SELECT that record (for the user to verify) if I don't
know what the primary key was when it was inserted?

Is the only way to not use autoincrement and choose the primary key value
myself?

Thanks


There is a LAST_INSERT_ID function that does this, though I don't use
it as apparently you can point it to a specific table (at least in the
documentation examples). So I do this by doing something like:
SELECT MAX(ID) AS ID FROM table WHERE addedby = 'me'
with the WHERE clause filtering on a field value that was just
inserted so that I can be sure it is looking at only the records that
I added myself.
Jul 19 '05 #6
Ray
"Gabriel Harrison" <Ga*****@gabrielharrison.co.uk> wrote in message news:<bj**********@news6.svr.pol.co.uk>...
Hi,

I have a mysql db with with autoincrementing primary keys in each table. I
want to allow a user to add a record using a php webpage. Once the page is
added how can I re SELECT that record (for the user to verify) if I don't
know what the primary key was when it was inserted?

Is the only way to not use autoincrement and choose the primary key value
myself?

Thanks


There is a LAST_INSERT_ID function that does this, though I don't use
it as apparently you can point it to a specific table (at least in the
documentation examples). So I do this by doing something like:
SELECT MAX(ID) AS ID FROM table WHERE addedby = 'me'
with the WHERE clause filtering on a field value that was just
inserted so that I can be sure it is looking at only the records that
I added myself.
Jul 19 '05 #7

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

Similar topics

0
by: Gabriel Harrison | last post by:
Hi, I have a mysql db with with autoincrementing primary keys in each table. I want to allow a user to add a record using a php webpage. Once the page is added how can I re SELECT that record...
17
by: Philip Yale | last post by:
I'm probably going to get shot down with thousands of reasons for this, but I've never really heard or read a convincing explanation, so here goes ... Clustered indexes are more efficient at...
2
by: Devesh Aggarwal | last post by:
Hi, I have a backup and restore module in my project. The backup uses a typed dataset object (XSD) to get the data from database and creates a xml file as the backup file (using the WriteXml...
2
by: jsfromynr | last post by:
Hi all, I have two tables CREATE TABLE ( NOT NULL , NULL , -- CURRENT DESIGNATION OF EMPLOYEE NOT NULL ) ON GO
6
by: Andreas | last post by:
Hello list, what about uniqueness of inherited primary keys ? eg you have : create table objects ( id int4, date_created timestamp(0), primary key (id)
7
by: Ilan Sebba | last post by:
I am trying to add a record using SQL. My problem is that the primary keys are foreign keys, and these foreign keys are autonumbers. I therefore do not know the primary keys of the record I am...
2
by: Matt Mercer | last post by:
Hi all, Hope I am not posting too many questions...cannot find the answer anywhere, though. I need to know when I insert a record into my database table what the primary key is for that record....
13
by: pb648174 | last post by:
Whenever I want help on a query, I get told my design is wrong, So this time I'm posting a message during the design phase: How am I going to perfectly design the following? We want to be able...
2
by: Bob | last post by:
This is the snippet of code Dim myConnString As String = "Integrated Security=SSPI;Packet Size=4096;Data Source=MyServer;" & _ "Initial Catalog=MyDatabase; " & _ "Persist Security...
2
by: alexs | last post by:
Chaps, I'm starting to play with db2 V9.1 and am writing a stored procedure to manage accounting records from oiur RADIUS server. I've got an XML aware table with an auto increment primary...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.