Which id...



  • Probably not worthy of a Code Sod given that this was written by student. Anyway thought this might bring a smile to everyones face.

    (The variables value are sane before anyone asks)

    $query="INSERT INTO currentauction VALUES ('','$desc','$minbid','$datetime','0','$ID')";    // insert into auctionhistory
    mysql_query($query);
    // get new ItemID and give to seller for their reference.
    $query="Select ItemID, SellerID FROM currentauction";
    $result=mysql_query($query);
    $newItemID=0;
    while ($row=mysql_fetch_array($result)) {
    $tempID=$row['ItemID'];
    $tempSellID=$row['SellerID'];
    if ($tempID > $newItemID and $tempSellID==$ID) {
    $newItemID=$tempID;
    }
    }


  • Looks like a

    SELECT MAX(ItemID) FROM currentauction WHERE SellID='$ID'

    but I'm no php/mysql expert...



  • @Kokuma said:

    Looks like a

    SELECT MAX(ItemID) FROM currentauction WHERE SellID='$ID'

    but I'm no php/mysql expert...

     Well, I said that, but there even be a better way like the MS-SQL "SELECT @@IDENTITY" that returns the last inserted id. Is there an equivalent in mysql ?



  • Heh, mysql_insert_id() would do the trick nicely. Gotta love students.



  • @Kokuma said:

    @Kokuma said:

    Looks like a

    SELECT MAX(ItemID) FROM currentauction WHERE SellID='$ID'

    but I'm no php/mysql expert...

     Well, I said that, but there even be a better way like the MS-SQL "SELECT @@IDENTITY" that returns the last inserted id. Is there an equivalent in mysql ?


    I don't know if there is a generic way to do it, but there is mysql_insert_id() from php.


  • @Kokuma said:

    @Kokuma said:

    Looks like a

    SELECT MAX(ItemID) FROM currentauction WHERE SellID='$ID'

    but I'm no php/mysql expert...

     Well, I said that, but there even be a better way like the MS-SQL "SELECT @@IDENTITY" that returns the last inserted id. Is there an equivalent in mysql ?

     You know that @@Identity is a very unsecure way to fetch the right ID? As soon as you have an Trigger that inserts something into another table, @@Identety  can fail to return the ID that you want. Since it will allways return the last generated ID für your Connection, and that might be one from the trigger... You should consider using Scope_Identity instead



  • @rdrunner said:

    You know that @@Identity is a very unsecure way to fetch the right ID? As soon as you have an Trigger that inserts something into another table, @@Identety  can fail to return the ID that you want. Since it will allways return the last generated ID für your Connection, and that might be one from the trigger... You should consider using Scope_Identity instead

    Thanks for the tip ! I'll remember it !



  • @mallard said:

    @Kokuma said:
    @Kokuma said:

    Looks like a

    SELECT MAX(ItemID) FROM currentauction WHERE SellID='$ID'

    but I'm no php/mysql expert...

     Well, I said that, but there even be a better way like the MS-SQL "SELECT @@IDENTITY" that returns the last inserted id. Is there an equivalent in mysql ?


    I don't know if there is a generic way to do it, but there is mysql_insert_id() from php.


    The generic way to do it in MySQL is SELECT LAST_INSERT_ID();

    That's unique to your own connection.


Log in to reply