MANUAL_INCREMENT: a parable



  • I've inherited a pretty big project (50k+ lines of code)......from an otherwise intelligent developer(I think), I noticed this gem:

    for($i=11001; $i<12500; $i++){
        $sql="INSERT INTO tbl_orders (total) VALUES ('50000000')";
        mysql_query($sql) or die(mysql_error());
    }

    Any guesses on what that's supposed to do?  Knowing that tbl_orders has an auto_increment field "id" helps.

    Using this web app, we can enter in our customer's orders.  The problem, though, was that our current order ID (outside the system) at the time this function was used was 12500.  That's right, this is the equivalent of:

    ALTER TABLE tbl_orders AUTO_INCREMENT = 12500;

     
    Take note that this function has been used more than once - it had to AT LEAST be used to "auto increment" up to 11000.  And I know for a fact we had the developer start us off at a number less than 12500 just recently, then we added some orders, found bugs, he worked on the code while we were manually taking our orders (so the DB order ID was being left behind).  Then once the developer was done fixing the bugs - he would run this function to catch the DB order id back up to where we said we were, so we could start putting orders in again.

    More things to note: The table has 63 fields but that's beside the point.

    Let's not overlook the fact that the table does not have any "total" fields with "50000000" in them, which means the developer then went into the database and ran a local query to:

    DELETE FROM tbl_orders WHERE total=50000000;

    That makes a grand total of 12500 + 12500 = 25,000 queries instead of 1.

     
    p.s. I was hired to maintain this monstrosity many months after this developer from a consulting company started work on it.  You'll be hearing more from me.
     



  • I make it 1500; 1499 for the inserts, and 1 delete... or did I micount.

    Still a major WTF, but that makes it 1:1



  • raluth is correct, because i starts at a weird value



  • And how did i get to such a strange value?

     

    First run:

    for i = 0 to 11000

     

    we use it, bugs need fixing, he fixes them and catches up the DB to coincide with what our order ID is in house. 

     

    this time he makes a small edit:

    for i = 11001 to 12500


    That is 12,500 inserts.  I don't know much about SQL, so I only assumed that a DELETE on N records counts as N number of queries (making it 25000 total).  Either way, I know enough to realize there's an AUTO_INCREMENT value.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.