473,383 Members | 1,801 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,383 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 16702
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...
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
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...

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.