Skip navigation.
Home

When Newbies Attack!

DB Hoopla

I think inexperienced web programmers all make common DB WTFs when starting out. Jim Grill sent in a prime example from a project that he inherited. I'm sure we've all seen similar code before and we've all said, "wtf?!", if not "ytf?!"

<?php
$query
= 'SELECT * FROM sometable';
$result = mysql_query($query,$connection);
$count = mysql_num_rows($result);
?>

It should be obvious what's wrong in the example. To count the number of rows all data is needlessly requested and the rows counted in PHP. As the table grows these three lines will get slower and slower. Most people take data transfer from the DB server for granted. However we always should be as efficient as possible, since little things can quickly multiply into big problems.

The fix is relatively simple: <?php
$query
= 'SELECT COUNT(*) FROM sometable';
$result = mysql_query($query,$connection);
list(
$count) = mysql_fetch_array($result);
?>

Using count(*) will return just the number of rows. Much more efficient.

1 + 1 = 3!?

Wonky Code
More crazy code from my world. This is a pretty simple function, but WTF does 3 mean in the code?
  function checkAdminBrokersCorner($fran_id,$admin) {
    if (($admin >= 3) || ($fran_id == 208)) {
      return true;
    }
    return false;
  }
The $fran_id == 208 is fairly easy to figure out. It likely refers to a database ID number. However $admin >= 3 is a little harder to determine what it means.