472,145 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

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 41534
>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 Saiars | last post: by
reply views Thread by leo001 | last post: by

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.