/    Sign up×
Community /Pin to ProfileBookmark

best way to get auto-increment ID from the mySQL record that was just inserted

Hi

I’m using one PHP page to handle several stages of a customer registration process for training sessions that my organization sponsors. A switch statement looks for certain elements in the $_POST array, and calls the appropriate function for that stage of the process.

After the user registers the organization that will pay the fees, the training participants are entered by the user one at a time. At each of these iterations, I want to display the organization information that was entered at the first stage.

Using a different language, in a different life, I was able to add a record to a table that has an auto-increment ID field, and grab the ID value while I was there. It made it very easy to store that value in a variable and use it to query the DB and display the information later. Can this be done with PHP/mySQL?

I have thought of putting the information that needs to be displayed into hidden form fields each time, and this seems very clunky to me.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@chazzyOct 26.2005 — not sure what you're asking for, but,

http://us2.php.net/manual/en/function.mysql-insert-id.php

gets you the id for the last insert statement executed.

otherwise you can use the auto_increment() function on an INT column in MYSQL.
Copy linkTweet thisAlerts:
@strBeanauthorOct 26.2005 — Yes, that's it exactly!

I thought there'd be a handy way to do it. Thanks!
Copy linkTweet thisAlerts:
@Doc_ThirstJul 25.2006 — Is there a mssql equivalent?
Copy linkTweet thisAlerts:
@Doc_ThirstJul 25.2006 — Found it. I used this, although I'm sure it could be smoother:

$recordset2 = mssql_query ("select @@identity", $handle);

while($arr = mssql_fetch_row($recordset2))

{

echo "$arr[0]";

}
Copy linkTweet thisAlerts:
@strBeanauthorJul 25.2006 — Your code looks like it will print all of the values in the table's first column.

The [URL=http://us3.php.net/manual/en/ref.mssql.php]manual[/URL] doesn't seem to have a direct equivalent in PHP, but you can always use SQL to get the largest value in the ID field after you run the insert query:

"select max(pkID) from table;"

assuming pkID is auto-increment...
×

Success!

Help @strBean spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...