473,815 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

retrieving new record ID?

I'm making a web-site thats backended by php-2-mysql. A common problem seems
to be when processing form data, I use information early in the form to
create a new record in a main table. I then immediately need the
auto_incremente d Key-ID of the new record to put other information in
related tables.

In the past i've gotten that ID by just doing a SELECT with a WHERE that
matches all the values i've just INSERTED, but it seems a cumbersome
method.

Is there a better or 'right-way' to have this ID returned to me?

Thanks,

Ravenslay3r
Jul 20 '05 #1
2 1541
Ravenslay3r wrote:
Is there a better or 'right-way' to have this ID returned to me?


Many ways:
http://dev.mysql.com/doc/mysql/en/Ge...unique_ID.html

But you propably want to use this:
http://dev.mysql.com/doc/mysql/en/mysql_insert_id.html

Because:
"The value of mysql_insert_id () is affected only by statements issued
within the current client connection. It is not affected by statements
issued by other clients."

Which means, that if you have a php script like this:

<?php
open_connection _to_mysql()
insert_row_into _table()
call mysql_insert_id ()
insert_row_into _second_table()
close_connectio n()
?>

It is safe to use mysql_insert_id (), even when there would be multiple
clients using the same script at the same time. Because client would get
the last id from her/his query, not from others queries.

But if you would use "select max(id) from table" or similar, you would
get wrong behavious if two clients would happen to be doing the same
thing at the same time.
Jul 20 '05 #2
Ravenslay3r wrote:
Is there a better or 'right-way' to have this ID returned to me?


Many ways:
http://dev.mysql.com/doc/mysql/en/Ge...unique_ID.html

But you propably want to use this:
http://dev.mysql.com/doc/mysql/en/mysql_insert_id.html

Because:
"The value of mysql_insert_id () is affected only by statements issued
within the current client connection. It is not affected by statements
issued by other clients."

Which means, that if you have a php script like this:

<?php
open_connection _to_mysql()
insert_row_into _table()
call mysql_insert_id ()
insert_row_into _second_table()
close_connectio n()
?>

It is safe to use mysql_insert_id (), even when there would be multiple
clients using the same script at the same time. Because client would get
the last id from her/his query, not from others queries.

But if you would use "select max(id) from table" or similar, you would
get wrong behavious if two clients would happen to be doing the same
thing at the same time.
Jul 20 '05 #3

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

Similar topics

4
1281
by: Justin | last post by:
Using SQL server and VS.NET with ASP.NET/C# how can I retrieve the Key ID of a record right after creating that record in the same event? Do I need to close and reopen the connection and requery? Thanks, Justin.
1
3899
by: tangus via DotNetMonster.com | last post by:
Hello all, I'm really struggling with getting some Active Directory code to work in ASP.NET. Can you please provide assistance? I am executing the following code: Dim enTry As DirectoryEntry = New DirectoryEntry("LDAP://domain") Dim mySearcher As New DirectorySearcher(enTry) Dim resEnt As SearchResult mySearcher.Filter = ("(objectClass=*)") mySearcher.SearchScope = SearchScope.Subtree
0
2075
by: Andy | last post by:
Hi All. I'm working for a company that has set out a guideline for retrieving data from a database. Nobody can explain to me the reason for the following. When retrieving a set of records from database to a VB.net app, they retrieve the database fields as a record set eg. "select name, suburb from myTable"
6
1817
by: Duderino82 | last post by:
I was wondering if there is a way to collect the names of the fields from a specific table. I think the soluction is to be researched in the sql code but maybe someone knows of a way to o so directly from php. Example. Table: Categories Field1: type1 Field2: type2 Field3: type3 Field4: type4
10
4129
by: Bhavna | last post by:
I am using a Replace function to replace single quotes with double when submitting a text field in the database i.e. Replace (q, "'", "' ' ") which works fine. When I retrieve the field from the database which has apostrophe I am getting 'Object expected' error message. Is there a way to fix this? Thanks, Bhavna
11
2037
by: Nemisis | last post by:
Hi everyone, sorry if this post gets really long, i just wanna make sure i fully explain what i am trying to do. I am new to OOP and .net 2.0, so if this is obvious, i am sorry. I have wrote a data access layer, which contists of separate servicebases for each object within my database. CompanyDataService (contains CRUD methods) ContactDataService (contains CRUD methods)
7
4708
by: rfinch | last post by:
Very new to this but using the MS working with dynamics CRM 3.0 book to run web application to retrieve lead records from CRM 3.0. Have followed the book instructions on page 380-382. But am getting the following in ie 7. Compiler Error Message: CS0234: The type or namespace name 'CrmSdk' does not exist in the class or namespace 'WorkingWithCrm' (are you missing an assembly reference?)
15
3541
by: gunnar.sigurjonsson | last post by:
I´m having some problem retrieving identity value from my newly inserted row into a view. I have two tables T1 and T2 which I define as following CREATE TABLE T1 ( id BIGINT GENERATED ALWAYS AS IDENTITY ( START WITH 1
9
35585
ADezii
by: ADezii | last post by:
One question which pops up frequently here at TheScripts is: 'How do I retrieve data from a Recordset once I've created it?' One very efficient, and not that often used approach, is the GetRows() Method of the Recordset Object. This Method varies slightly from DAO to ADO, so for purposes of this discussion, we'll be talking about DAO Recordsets. The ADO approach will be addressed in the following Tip. We'll be using a Query, consisting of 5...
3
43549
ADezii
by: ADezii | last post by:
Last Tip, we demonstrated the technique for retrieving data from a DAO Recordset, and placing it into a 2-dimensional Array using the GetRows() Method. This week, we will cover the same exact Method (GetRows()), but only as it applies to an ADO Recordset. Although there are similarities in the 2 methodologies, the ADO Method offers 2 more Optional Arguments, is a little more complex, and of course, the syntax is different in creating the...
0
9736
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9611
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10408
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10428
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9226
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5710
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4359
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3030
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.