Connecting Tech Pros Worldwide Forums | Help | Site Map

Using LAST_INSERT_ID()

Vic Spainhower
Guest
 
Posts: n/a
#1: Oct 1 '05
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";



Gordon Burditt
Guest
 
Posts: n/a
#2: Oct 1 '05

re: Using LAST_INSERT_ID()


>I am trying to get the value of the Auto-Incremented key that was assigned[color=blue]
>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).[/color]

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.
[color=blue]
>..............................
>$_SESSION["entryID"] = (mysql_query ('SELECT LAST_INSERT_ID() FROM
>tblShowEntries'));
>
>$entryID = $_SESSION["entryID"];
>echo "value of entryID: $entryID";[/color]

Gordon L. Burditt
Vic Spainhower
Guest
 
Posts: n/a
#3: Oct 2 '05

re: Using LAST_INSERT_ID()


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" <gordonb.f5slx@burditt.org> wrote in message
news:11jti809ho0re4d@corp.supernews.com...[color=blue][color=green]
> >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).[/color]
>
> 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.
>[color=green]
>>..............................
>>$_SESSION["entryID"] = (mysql_query ('SELECT LAST_INSERT_ID() FROM
>>tblShowEntries'));
>>
>>$entryID = $_SESSION["entryID"];
>>echo "value of entryID: $entryID";[/color]
>
> Gordon L. Burditt
>[/color]


Kim André Akerĝ
Guest
 
Posts: n/a
#4: Oct 2 '05

re: Using LAST_INSERT_ID()


Vic Spainhower wrote:
[color=blue]
> 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;
> }[/color]

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ĝ
- kimandre@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
Closed Thread