Re: last_insert_id function: I did already

From: Date: Wed, 24 Jan 2001 10:18:27 +0000
Subject: Re: last_insert_id function: I did already
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-36383@lists.php.net to get a copy of this message
Hi, To use mySQL's internal function, try something like this: mysql_query("insert into mytable values(1,2,3)"); $rs = mysql_query("select LAST_INSERT_ID() from mytable"); $theLastInsertedID = mysql_result($rs,0,0); ""Jacky@lilst"" <jacky@activelifestyle.com> a écrit dans le message news: 05f701c0865b$a2fe61e0$6400a8c0@alsmpdc... > I did try already. > What I did was I assign value from LAST_INSERT_ID(); onto a virable after I > run the first insert query the same way we do to mysql_insert_id > and the result when the page is executed, it said " unidentified function" > which of course because php does not know LAST_INSERT_ID like you said > previously. > By the way, what even drive me crazy more is that with using > mysql_insert_id();, it returns me value 0 (zero) which I suspected it comes > from the fact that data type of that field is BIGINT, correct me if I'm > wrong. > any thought? > Jack > jacky@activelifestyle.com > "There is nothing more rewarding than reaching the goal you set for > yourself" > ----- Original Message ----- > From: Maxim Maletsky <maxim.maletsky@japaninc.net> > To: 'Jacky@lilst' <jacky@activelifestyle.com>; 'Mark Lipscombe' > <mark@itgarp.com> > Cc: <php-general@lists.php.net> > Sent: Wednesday, January 24, 2001 4:06 AM > Subject: RE: [PHP] last_insert_id function > > > > I am not really sure about this one, but the first thing I would do is > that > > I would try it. > > > > It could be though ... > > > > Cheers, > > Maxim Maletsky > > > > -----Original Message----- > > From: Jacky@lilst [mailto:jacky@activelifestyle.com] > > Sent: Thursday, January 25, 2001 7:53 AM > > To: Maxim Maletsky; 'Mark Lipscombe' > > Cc: php-general@lists.php.net > > Subject: Re: [PHP] last_insert_id function > > > > > > Can we store the value retrieved by last_insert_id for later use? say we > > want to related 3 queries together and under that case, we will then need > to > > store the first "last_insert_id value" somewhere and then retrive the > second > > "last_insert_id value" from the second insert query before both values > will > > be inserted into the third query. > > Can we do that? If not, I will have to come back to mysql_insert_id(); but > > it won't work with a field with data type BIGINT, will it? So I guess I > > would have to change the data type too, right? > > please enlighten me about this > > Thank > > Jack > > jacky@activelifestyle.com > > "There is nothing more rewarding than reaching the goal you set for > > yourself" > > ----- Original Message ----- > > From: Maxim Maletsky <maxim.maletsky@japaninc.net> > > To: 'Mark Lipscombe' <mark@itgarp.com> > > Cc: 'Jacky@lilst' <jacky@activelifestyle.com>; <php-general@lists.php.net> > > Sent: Wednesday, January 24, 2001 3:36 AM > > Subject: RE: [PHP] last_insert_id function > > > > > > > no it will keep it very well. In fact most common errors with this > > function > > > is that it still keeps the same value when you do something wrong. > > > > > > This issue is described in more details on every single manual that has > > > LAST_INSERT_ID() in it - read where you found it ... it is written there > > ... > > > I am sure ... > > > > > > However, on my opinion this function is very reliable. I use it a lot > when > > > have to create a script which splits some few huge tables into a dozen > of > > > well designed ones. Because redesigning the database you really have to > > > relate your data to each other. LAST_INSERT_ID() does a good job. > > > > > > Cheers, > > > Maxim Maletsky > > > > > > -----Original Message----- > > > From: Mark Lipscombe [mailto:mark@itgarp.com] > > > Sent: Thursday, January 25, 2001 5:28 AM > > > To: Maxim Maletsky > > > Cc: 'Jacky@lilst'; php-general@lists.php.net > > > Subject: RE: [PHP] last_insert_id function > > > > > > > > > Someone jump in here and correct me if I'm wrong, but by the time you > > > get around to executing the second query, LAST_INSERT_ID() from MySQL > > > isn't necessarily going to be the desired value, because another record > > > may well have been inserted in that time? > > > > > > On 24 Jan 2001 18:25:27 +0900, Maxim Maletsky wrote: > > > > for example: > > > > > > > > $SQL = "INSERT INTO users SET name='Maxim', > > > > surname='Maletsky'"; > > > > > > > > now you have to insert into another table where you need to relate > that > > > user > > > > to the entry: > > > > > > > > $SQL2 = "INSERT INTO questions SET question='how did you sleep?', > > > > made_by=LAST_INSERT_ID()"; > > > > > > > > LAST_INSERT_ID() will be here equal to the auto_incremented id of the > > > first > > > > $SQL statement. > > > > > > > > Hope this helps, > > > > Maxim Maletsky > > > > > > > > > > > > -----Original Message----- > > > > From: Jacky@lilst [mailto:jacky@activelifestyle.com] > > > > Sent: Thursday, January 25, 2001 7:16 AM > > > > To: php-general@lists.php.net > > > > Subject: [PHP] last_insert_id function > > > > > > > > > > > > I got this quote right out of the php manual. My Id field happen to be > > > type > > > > BIGINT as it said so I tried using LAST_INSERT_ID(); and turn out to > be > > > > error said "unidentified function". Any clue? > > > > ******************************************* > > > > mysql_insert_id() converts the return type of the native MySQL C API > > > > function mysql_insert_id() to a type of long. If your AUTO_INCREMENT > > > column > > > > has a column type of BIGINT, the value returned by mysql_insert_id() > > will > > > be > > > > incorrect. Instead, use the internal MySQL SQL function > > LAST_INSERT_ID(). > > > > ******************************************** > > > > Jack > > > > jacky@activelifestyle.com > > > > "There is nothing more rewarding than reaching the goal you set for > > > > yourself" > > > > > > > > -- > > > > PHP General Mailing List (http://www.php.net/) > > > > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > > > > For additional commands, e-mail: php-general-help@lists.php.net > > > > To contact the list administrators, e-mail: > php-list-admin@lists.php.net > > > > > > > > > > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > > For additional commands, e-mail: php-general-help@lists.php.net > > To contact the list administrators, e-mail: php-list-admin@lists.php.net > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#36383) next »