WTF...Bad Code


March 27th, 2007

While rewriting an old product, I figured I would go back and see how the last team did it. Some of the old team is still at Unnamed Company, but they are the smart ones. Dig through this old php code and see if it makes you laugh too. I love the comment on the first line.

//If their is no pi_menu_arent_id set it
if (!IsSet($video_id))
{
	$video_id[] = 2; //Default for all productname

echo "";
	
foreach ($video_id as $value) 
{
   	$global_dbh = mysql_connect('localhost', 'root', '*****');
        mysql_select_db('dbname');

        //Get Video Info
       $query_string = "SELECT video_id, video_filename, video_path, video_language, video_description, video_length, video_codec,        video_audio_codec, video_bitrate, video_dts, video_project FROM	videos WHERE  video_id = $value ";
      $result_id    = mysql_query($query_string);
      $data         = mysql_fetch_array ($result_id);
     //echo mysql_errno().": ".mysql_error()."
"; //Get Speed setting $query_string1 = "select user_video_speed, user_lang FROM users WHERE user_ip = '$REMOTE_ADDR';"; $result_id1 = mysql_query($query_string1); $data1 = mysql_fetch_array ($result_id1); ... ... ...
In case you didn't see the major WTF in there, I'll spell it out for you. Notice the foreach loop, he was looping through a collection that was retrieved from a database. Then immediately inside the loop, opens another connection to the same database. So for every record you got from the first database call, it opens a new connection, selects the database, does some stuff, disconnects and starts all over again. Ughh and they are connecting to the db with the root user also. Ohh and let me tell you, the password wasn't strong. Praise Geeeebus for RoR!!!...and that the guy, who wrote this code, quit.

Leave a Reply