List:Internals« Previous MessageNext Message »
From:Paul DuBois Date:October 1 2000 8:17pm
Subject:Re: Value of an automatically generated ID
View as plain text  
At 7:01 PM +0200 09-28-2000, FOURNIER Jacques - GRE wrote:
>How to get the value of an automatically generated ID ?
>
>I have read the following chapter of the mySQL documentation :
>18.5 How to get the value of an AUTO_INCREMENT column in ODBC
>but the solution given doesn't work for me !
>
>This chapter said that just after an insert command, the following
>command must be done :
>SELECT LAST_INSERT_ID();
>but no data is selected with this command.
>
>Here is my (php) code, flightNum is my auto_increment column :
>------------
>   $sql  = "INSERT INTO cfdFlights (flightNum,licence) VALUES
>(NULL,'$licenceSv')";
>   $sql2         = "SELECT LAST_INSERT_ID()";
>   $result       = mysql_query($sql,$conn);
>   $resultLast   = mysql_query($sql2,$conn);
>
>    $newFlightNum = mysql_result($resultLast, 0, "flightNum");
>------------
>And there is nothing in my variable $newFlightNum after that.
>
>What can I do ?
>
>Even if this solution works, is it a good solution ? Because if between
>my insert command and my select command, another program make an insert
>command, I think I have the risk to have a wrong ID...


1) Your program has no error checking.  You don't know if your INSERT
statement even works.
2) LAST_INSERT_ID() is client-specfic. It doesn't matter whether or not
other clients insert rows.
3) This question doesn't really belong on the developer's list; it doesn't
have anything to do with developing MySQL.

-- 
Paul DuBois, paul@stripped
Thread
Value of an automatically generated IDFOURNIER Jacques - GRE1 Oct
  • Re: Value of an automatically generated IDPaul DuBois1 Oct