/    Sign up×
Community /Pin to ProfileBookmark

is there a more concise way to this …?

Hi
is there an easy way to find the primary key of the last record you have just entered to a table?

and this is the senario: (I should say that all is about PHP and mysql)
I have 2 tables (or more). I enter a record to the first one and just then I need its primary key to enter it to another table (for relational purposes).

What I do now is like this:

first I insert the record to the first table.
then I make a query and find the largest primary key which is the one that I just added.
and at last I put that key in my second table (among the rest of data I need to put in second table.)

I guess there must be a more concise way so that I wouldn’t make the query in the middle.

if you know a better way please tell me and if you would do the same tell me so that I don’t search. (I am certain that if master experts here don’t know a way there is no chance for me to find it)

Thanks.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@ShrineDesignsFeb 16.2005 — if you are using AUTO_INCREMENT on the column, you can use mysql_insert_id() or sql:SELECT LAST_INSERT_ID()NOTE: the returned values from both are not the same, see http://dev.mysql.com/doc/mysql/en/getting-unique-id.html for further details
×

Success!

Help @amahmood 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.7,
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,
)...