473,402 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 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 41641
>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mehul Patel | last post by:
Our .Net team have been pondering about using keyword. We are using streams FileStream and BufferedStream. We use using keyword at FileStream, and not BufferedStream which wraps FileStream. So...
2
by: rawCoder | last post by:
Hi All, I have a *.cer file, a public key of some one and I want to encrypt some thing using this public key. Can someone point me to a sample code for Encrypting some file using...
1
by: Mike | last post by:
When trying to compile (using Visual Web Developer 2005 Express Beta; frameworkv2.0.50215 ) the source code below I get errors (listed below due to the use of ICallBackEventHandler. Ultimately I...
4
by: Marshall Mills | last post by:
As I understand it, loaded statement, a using declaration should be all I need to see an enum from within a namespace. The below code works fine with class, struct, and union. What gives? As the...
10
by: Christopher Benson-Manica | last post by:
Why can't I use a class destructor in a using declaration: using MyClass::~MyClass; ? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org ...
5
by: Enos Meroka | last post by:
Hallo, I am a student doing my project in the university.. I have been trying to compile the program using HP -UX aCC compiler, however I keep on getting the following errors. ...
8
by: acb | last post by:
Hi, I wrote a DLL Component (using Visual Studio 2005) and managed to include it into a C# Console application. I am now trying to include this component into a Web project. I copy the DLL...
0
by: Eugene Anthony | last post by:
The problem with my coding is that despite removing the records stored in the array list, the rptPages repeater control is still visible. The rptPages repeater control displayes the navigation...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
8
by: =?Utf-8?B?Q2hyaXMgSGFsY3Jvdw==?= | last post by:
Hi there I've successfully added some .NET validation controls to a page (using <asp:RequiredFieldValidator ...), however when I try to set the 'display' property to 'dynamic', my page then...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.