By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,326 Members | 2,873 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,326 IT Pros & Developers. It's quick & easy.

Return an primary key value after insert query?

P: n/a
Hi,

I'm fairly new to mysql, but I have a reasonable knowledge of PHP.

I just want to know is theres a simple way to return a primary key
value for a record I've just inserted?

In other environments I'd set a flag using the current time stamp which
is inserted with the record.

Then I'd select on that timestamp and get the primary key value.

Is there a simpler way?

Jules.

Jul 11 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Julian Moorhouse wrote:
Hi,

I'm fairly new to mysql, but I have a reasonable knowledge of PHP.

I just want to know is theres a simple way to return a primary key
value for a record I've just inserted?

In other environments I'd set a flag using the current time stamp which
is inserted with the record.

Then I'd select on that timestamp and get the primary key value.

Is there a simpler way?

Jules.
If your primary key is made using AUTO_INCREMENT, you may use the
mysqli_insert_id function.

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

-david-

Jul 11 '06 #2

P: n/a
Hi,

Thanks for your reply.

I don't think I have mysqli on my ISPs server.

Jules.

Jul 11 '06 #3

P: n/a
Rik
Julian Moorhouse wrote:
Hi,

Thanks for your reply.

I don't think I have mysqli on my ISPs server.
normal mysql_insert_id() works too.

Tip: if you're new to something in PHP, getting the applicable chapter from
the function reference is very handy. In this case,
http://www.php.net/manual/en/ref.mysql.php, which lists all functions
associated with mysql.

Grtz,
--
Rik Wasmus
Jul 11 '06 #4

P: n/a
Ah..

echo mysql_insert_id();

works too

Thanks,

Jules.

Jul 11 '06 #5

P: n/a
"Julian Moorhouse" <mi*********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Hi,

Thanks for your reply.

I don't think I have mysqli on my ISPs server.

Same difference. The same fucntion exists also for "plain" mysql, just
remove the extra "i". Ie. use mysql_insert_id().

--
"ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)
Jul 11 '06 #6

P: n/a
On Tue, 11 Jul 2006 12:39:11 +0200, Rik wrote:
>Julian Moorhouse wrote:
>Hi,

Thanks for your reply.

I don't think I have mysqli on my ISPs server.

normal mysql_insert_id() works too.

Tip: if you're new to something in PHP, getting the applicable chapter from
the function reference is very handy. In this case,
http://www.php.net/manual/en/ref.mysql.php, which lists all functions
associated with mysql.
.... *and* it has loads of really helpful hints & snippets about each
function. A veritable gold mine!

Adam.
Sep 25 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.