The replacement for Auto-Increment...



  • What's a programmer to do when the time clock table doesn't have an auto increment field? You concoct a ridiculous SQL statement in order to do it yourself! 

    <code>

        $query = "SELECT ID FROM time_clock WHERE right(ID,2)=99 ORDER BY ID desc LIMIT 1";
        $dbcre=new DB_cre;
        $dbcre->query($query);
            while($dbcre->next_record()) {
                $new_id=$dbcre->f("ID")+'100';
            }

    </code>

    What rubbish. First of all there is no ID value in this table that has an ending of "99" therefore no "new_id" is even generated. Second of all, he could have just done this..  

    <code>

       SELECT MAX(ID)+1 AS new_id FROM time_clock WHERE 1

    </code>


    It's only monday. Damn it all.


     



  • At least it doesn't use a cursor!
     



  • But he is using a function on a field inside there where clause....

     Thats worse then a cursor!



  • @neuralfraud said:

    <code>

       SELECT MAX(ID)+1 AS new_id FROM time_clock WHERE 1

    </code>

    Why the WHERE clause?!?

    BTW, if there is another way to do it (like real autoincrement fields or sequences), choose the other way."MAX(id)+1" begs for race-conditions. 



  • I would swear that MySQL often pukes if a where clause is not included, but you're right, so good catch. Still a WTF though, the solution is a little WTF-y but then again, there is simply no reason for this field not to have an auto increment ID. This is a commercial product, a commercial POS product. A commercial POS product coded in VB.net.

    The code highlighted here is from an interface to its time clock from our intranet. I'm not sure the code behind the POS is any better than the code highlighted in this thread.

     



  • Winner of the 2016 Presidential Election

    @neuralfraud said:

    I would swear that MySQL often pukes if a where clause is not included, but you're right, so good catch. Still a WTF though, the solution is a little WTF-y but then again, there is simply no reason for this field not to have an auto increment ID. This is a commercial product, a commercial POS product. A commercial POS product coded in VB.net.

    The code highlighted here is from an interface to its time clock from our intranet. I'm not sure the code behind the POS is any better than the code highlighted in this thread.

     


    I'll just mention that there are two equally valid expansions for the acronym POS, and often both are applicable.


Log in to reply
 

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