Skip navigation.
Home

How to burn a database in 3 steps...

DB Hoopla

Thanks to Au5lander for sending in this WTF he found on pastebin.com. I'm thinking that this might be some benchmarking code because it'll make the database work so hard!

The code basically goes like this:

  1. If record does not exist, insert data into the first column.
  2. If record exists, update the record with data for the second column
  3. Repeat for each additional column
  4. Watch database burn...

<?php
while(list($key,$value) = each ($arraywerk))
   {
       if($arraywerk[$key] != '')
       {
           //doorloop de colommen in de tabel
           for ($i = 0; $i < $numcolumns; $i++)
           {
               $keyQ = mysql_field_name($fields, $i);
               if ($key == $keyQ)
               {
                   if ($insertidstatus==false) {
                       //create the insert query
                       $sql = "INSERT INTO werkgever ($key) VALUES ('$value')";
                       mysql_query($sql, $socket);
                       $insertid = mysql_insert_id();
                       $insertidstatus=true;
                   } else {
                       $sql = "UPDATE werkgever SET $key='$value' WHERE vacatureID='$insertid'";
                       mysql_query($sql, $socket);
                   }
               }
           }
       }
   }
   $sql = "UPDATE werkgever SET werkgeverstatusID=0 WHERE vacatureID='$insertid'";
   mysql_query($sql, $socket);
?>

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Tastes like burning! Has a

Tastes like burning! Has a number of things that will make the code a pain to maintain.
  1. Not really a WTF, but one of my pet peeves: using huge if blocks in loops.

    if (empty($arraywerk[$key])) {
    continue;
    }

    is much cleaner and easier to read.
  2. Directly using mysql_*(). This is so messy. Use a DB abstraction layer.
  3. What in the hell is going on with mysql_field_name()?! Do the table's field order change in between the SELECT and this code?!
  4. Gotta love that O(n^n) loop-in-a-loop. How about:

    $fields = implode(',', array_keys($arraywerk));
    $values = implode("','", array_values($arraywerk));
    $query = "INSERT INTO werkgever ($fields) VALUES (\'$values\')";

I totally agree with #1.

  1. I totally agree with #1. It does makes much cleaner code rather than trying to locate that closing bracket dozens of lines down.
  2. I can't agree with this. I used to recommend that everybody use ADODB and Smarty. But after talking to Rasmus a long time ago he made some good points about whether a db abstraction layer is necessary. I mean how many people write PHP applications for more than one database? You still have to deal with cross database queries. The only time I recommend using a DB abstraction layer is if you're building a complex application and the convienence of an extra layer out weighs the performance hit.

Yet another WTF with Dutch co

Yet another WTF with Dutch comments. We're not all bad programmers you know! ;-)

BTW the translation for the comment is obviously 'Loop through columns in database'. And 'werkgever' means employer. I'd be amazed if the programmer still has one. :-)

I found an unoptimized page i

I found an unoptimized page inside my latest PHP project, and added a quick query counter. My eyes almost fell out when I saw this: "Page built in 15.529 seconds (SQL: 15141 queries)" I managed to optimize it from 15000 queries, down to 3 :)

Post new comment




*

  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <p> <br>
  • Web and e-mail addresses are automatically converted into links.