469,322 Members | 1,603 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,322 developers. It's quick & easy.

Using LAST_INSERT_ID()

Hello,

I am trying to get the value of the Auto-Incremented key that was assigned
using LAST_INSERT_ID(). However, when I execute the following PHP statements
after the insert I only get: "value of entryID: Resource id #7" after EVERY
insert (it never changes).

What am I doing wrong?

Thanks very much!

Vic
...............................
$_SESSION["entryID"] = (mysql_query ('SELECT LAST_INSERT_ID() FROM
tblShowEntries'));

$entryID = $_SESSION["entryID"];
echo "value of entryID: $entryID";
Oct 1 '05 #1
3 41366
>I am trying to get the value of the Auto-Incremented key that was assigned
using LAST_INSERT_ID(). However, when I execute the following PHP statements
after the insert I only get: "value of entryID: Resource id #7" after EVERY
insert (it never changes).
The return value of mysql_query is *NOT* the value for last_insert_id().
Fetch a row (e.g. using mysql_fetch_row) and then look at the column
you want, and *thats* the value you are looking for.
..............................
$_SESSION["entryID"] = (mysql_query ('SELECT LAST_INSERT_ID() FROM
tblShowEntries'));

$entryID = $_SESSION["entryID"];
echo "value of entryID: $entryID";


Gordon L. Burditt
Oct 1 '05 #2
Thanks Gordon,

The following code seems to work fine ....

$query = "SELECT LAST_INSERT_ID()";
$result = mysql_query($query);
if ($result) {
$nrows = mysql_num_rows($result);
$row = mysql_fetch_row($result);
$lastID = $row[0];
$_SESSION["entryID"] = $lastID;
}
Vic
"Gordon Burditt" <go***********@burditt.org> wrote in message
news:11*************@corp.supernews.com...
I am trying to get the value of the Auto-Incremented key that was
assigned
using LAST_INSERT_ID(). However, when I execute the following PHP
statements
after the insert I only get: "value of entryID: Resource id #7" after
EVERY
insert (it never changes).


The return value of mysql_query is *NOT* the value for last_insert_id().
Fetch a row (e.g. using mysql_fetch_row) and then look at the column
you want, and *thats* the value you are looking for.
..............................
$_SESSION["entryID"] = (mysql_query ('SELECT LAST_INSERT_ID() FROM
tblShowEntries'));

$entryID = $_SESSION["entryID"];
echo "value of entryID: $entryID";


Gordon L. Burditt

Oct 2 '05 #3
Vic Spainhower wrote:
Thanks Gordon,

The following code seems to work fine ....

$query = "SELECT LAST_INSERT_ID()";
$result = mysql_query($query);
if ($result) {
$nrows = mysql_num_rows($result);
$row = mysql_fetch_row($result);
$lastID = $row[0];
$_SESSION["entryID"] = $lastID;
}


Please note that the PHP function mysql_insert_id() will return the
same as you're trying to fetch.

$lastID = mysql_insert_id();

http://php.net/mysql_insert_id

--
Kim Andrť AkerÝ
- ki******@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
Oct 2 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Mehul Patel | last post: by
2 posts views Thread by rawCoder | last post: by
4 posts views Thread by Marshall Mills | last post: by
10 posts views Thread by Christopher Benson-Manica | last post: by
5 posts views Thread by Enos Meroka | last post: by
8 posts views Thread by acb | last post: by
reply views Thread by Eugene Anthony | last post: by
8 posts views Thread by =?Utf-8?B?Q2hyaXMgSGFsY3Jvdw==?= | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.