RE: mysql: get id of just-added record

  • From: "Homme, James" <james.homme@xxxxxxxxxxxx>
  • To: "programmingblind@xxxxxxxxxxxxx" <programmingblind@xxxxxxxxxxxxx>
  • Date: Thu, 12 May 2011 06:36:16 -0400

Hi,
In case you didn't realize this, you can type special URL's in your web browser 
and get help on PHP stuff. For example, I just typed in 
http://us2.php.net/mysqli. I got back a page that gives me a whole lot of stuff 
to read about this extension.

Jim

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx 
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Alex Hall
Sent: Wednesday, May 11, 2011 9:15 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: Re: mysql: get id of just-added record

Well, that certainly sounds easier! Thanks, I never knew this one existed.

On 5/11/11, Jeff Berwick <mailinglists@xxxxxxxxxxxx> wrote:
> You can use the mysql_insert_id() function.  If you are using the newer
> mysqli libraries, I believe the function is mysqli_insert_id().
>
> That will return the id of the last inserted record.
>
> Hth,
> Jeff
>
> On 2011-05-11, at 7:44 PM, Alex Hall wrote:
>
>> Hi all,
>> I have a mysql question. My php script is adding an article to a
>> database. However, it must also add one or more rows to an
>> article_products table, which consists of just two fields, an article
>> id and a product id, which form a composite primary key. The problem
>> is that the article_products table can't be updated until the script
>> knows the id of the article being added, so I need two separate
>> queries to create everything: create the article, then create all rows
>> necessary in the article_products table. My question, therefore, is:
>> what is the best way to get the id of the article just created? The id
>> is an auto_increment integer. I have so far thought about pulling the
>> first record when records are sorted in descending order by id, but it
>> seems like there might be a better way. If not, what would the syntax
>> be? I plan on using something like:
>> select article_id from articles sort by article_id desc limit 1;
>> Is there a better way? Thanks in advance.
>>
>> --
>> Have a great day,
>> Alex (msg sent from GMail website)
>> mehgcap@xxxxxxxxx; http://www.facebook.com/mehgcap
>> __________
>> View the list's information and change your settings at
>> //www.freelists.org/list/programmingblind
>>
>
> __________
> View the list's information and change your settings at
> //www.freelists.org/list/programmingblind
>
>


--
Have a great day,
Alex (msg sent from GMail website)
mehgcap@xxxxxxxxx; http://www.facebook.com/mehgcap
__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind


This e-mail and any attachments to it are confidential and are intended solely 
for use of the individual or entity to whom they are addressed.  If you have 
received this e-mail in error, please notify the sender immediately and then 
delete it.  If you are not the intended recipient, you must not keep, use, 
disclose, copy or distribute this e-mail without the author's prior permission. 
 The views expressed in this e-mail message do not necessarily represent the 
views of Highmark Inc., its subsidiaries, or affiliates.
__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

Other related posts: