Hi everyone,

can you check for me if my codes below are correct? i am having problem to retrieve my image file from mysql database. The errors start at "header" parts. I have no problem to store image file (mediumblob) in mysql database. The file shown are in HEX forms.

**********************************************************

<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
$a = mysql_connect("localhost","root","");
if (!$a){
die ("<br>ERROR in mysql_connect<br>");
} else
$db = mysql_select_db('test');
$query = "SELECT * FROM image";
echo " <br> line 16..... <br>";
$result = mysql_query($query) or die('Error, line 17 query failed');

if(mysql_num_rows($result) == 0)
{
echo "Database is empty <br>";
}
else
{
while(list($id, $name) = mysql_fetch_array($result))
{
echo " <br> line 25..... <br>";
echo "<a href=\"download.php?id=$id \"></a> <br>";
echo "line 31......";
}
}
mysql_close();
?>
</body>
</html>


<?php
$id = 1;
echo " <br> line 44..... <br>";
if("$id")
{
// if id is set then get the file with the id from database
echo " <br> line 50..... <br>";
$a = mysql_connect("localhost","root","");
if (!$a){
die ("<br>ERROR in mysql_connect<br>");
} else
$db = mysql_select_db('test');
echo " <br> line 58..... <br>";
//$id = $_GET;
$query = "SELECT name, type, size, content FROM image WHERE id = '$id'";
$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysql_fetch_array($result);
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content;
mysql_close();
exit;
} echo " cant enter line 47...";
?>

************************************************************
thanks :)

Recommended Answers

All 2 Replies

You should not send any output to the browser before

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");

Always header() should be the first to send output to the browser.

Try removing all the echo statements in that file above header()

hi buddies,
please explain me how to retrive the image from database,i am beginner in php.below i present my coding.if any error please explain about that.
by,
ur friend
kalpana

<?php
session_start();
$db=mysql_connect('localhost','root','');
mysql_select_db('registration',$db);

$se=mysql_query("select * from reg1 where username='".$_SESSION."'");

while($r=mysql_fetch_array($se))
{

?>

<form name="regform" method="post">

<table>
<tr><td><b>EMAIL</b></td><td><input type="text" value="<? echo $r; ?>" /></td></tr>
<tr><td>&nbsp;</td></tr>
<tr><td><b>USERNAME</b></td><td><input type="text" value="<? echo $r; ?>" /></td></tr>
<tr><td>&nbsp;</td></tr>
<tr><td><b>DISPLAYNAME</b></td><td><input type="text" value="<? echo $r; ?>" /></td></tr>
<tr><td>&nbsp;</td></tr>
<tr><td><b>FIRSTNAME</b></td><td><input type="text" value="<? echo $r; ?>" /></td></tr>
<tr><td>&nbsp;</td></tr>
<tr><td><b>MIDDLENAME</b></td><td><input type="text" value="<? echo $r; ?>" /></td></tr>
<tr><td>&nbsp;</td></tr>
<tr><td><b>LASTNAME</b><td><input type="text" value="<? echo $r; ?>" /></td></tr>
<tr><td>&nbsp;</td></tr>
<tr><td><b>PHONE</b></td><td><input type="text" value="<? echo $r; ?>" /></td></tr>
<tr><td>&nbsp;</td></tr>
<tr><td><b>PASSWORD</b></td><td><input type="password" value="<? echo $r; ?>" /></td></tr>
<tr><td>&nbsp;</td></tr>
<tr><td><b>RETYPEPASSWORD</b></td><td><input type="password" value="<? echo $r; ?>" /></td></tr>
<tr><td>&nbsp;</td></tr>
<tr><td><b>LANGUAGE</b></td><td><input type="text" value="<? echo $r; ?>" /></td></tr>
<tr><td>&nbsp;</td></tr>
<tr><td><b>COUNTRY</b></td><td><input type="text" value="<? echo $r; ?>" /></td></tr>
<tr><td>&nbsp;</td></tr>
<tr><td><b>STATES</b></td><td><input type="text" value="<? echo $r; ?>" /></td></tr>
<tr><td>&nbsp;</td></tr>
<tr><td><b>ZIPCODE</b></td><td><input type="text" value="<? echo $r; ?>" /></td></tr>
<tr><td>&nbsp;</td></tr>
<tr><td><b>IMAGE</b></td><td><input type="image" value="<? echo $r; ?>" /></td></tr>

</table>
</form>

<?

}
?>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.