Uploading image into mysql using php

I have this code but it does not work. I am not sure what I am doing wrong.

<?php
###############################
$username="";
$password="";
$database="";
###############################
$dbcnx = mysql_connect(localhost, $username, $password); 
mysql_select_db($database);

$path = "images";
$dir_handle = opendir($path) or die("Unable to open directory $path");

while ($file = readdir($dir_handle)) {
$filetyp = substr($file, -3);
if ($filetyp == 'gif' OR $filetyp == 'jpg') {
$handle = fopen($file,'r');
$file_content = fread($handle,filesize($file));
fclose($handle);
$encoded = chunk_split(base64_encode($file_content)); 
$sql = "INSERT INTO images SET sixfourdata='$encoded'"; 
mysql_query($sql);
}
}
closedir($dir_handle);
//images
$img = $_REQUEST["img"];
    $result = mysql_query('SELECT * FROM images WHERE imgid=" . $img ."'); 
    if (!$result) { 
      echo("<b>Error performing query: " . mysql_error() . "</b>"); 
      exit(); 
    } 
    while ($row = mysql_fetch_array($result)) { 
    $imgid = $row["imgid"];
    $encodeddata = $row["sixfourdata"]; 
    }


echo base64_decode($encodeddata);
mysql_close($dbcnx);
//echo("complete");
//mysql_close($dbcnx);
?>

I got this from a tutorial that I found here
http://www.spoono.com/php/tutorials/tutorial.php?id=42

I have tried to figure this out but I am not sure what I am doing wrong.