# Uploading image into mysql using php

**URL:** <https://forum.kirupa.com/t/uploading-image-into-mysql-using-php/219006>\
**Category:** Uncategorized\
**Created:** [March 13, 2007, 3:14am UTC](https://forum.kirupa.com/t/uploading-image-into-mysql-using-php/219006 "2007-03-13T03:14:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![alpharaven](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@alpharaven](https://forum.kirupa.com/u/alpharaven)\
**Post date:** [March 13, 2007, 3:14am UTC](https://forum.kirupa.com/t/uploading-image-into-mysql-using-php/219006/1 "2007-03-13T03:14:03Z")

</div>

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

```php
<?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](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.
