| | |
retrieving image from mysql database using php
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Dec 2006
Posts: 1
Reputation:
Solved Threads: 0
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['id'];
$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
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['id'];
$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
•
•
Join Date: Dec 2006
Posts: 6
Reputation:
Solved Threads: 0
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()
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()
![]() |
Similar Threads
- how to retrive image file from mysql databse using php (PHP)
- php5 and phpmyadmin?? (PHP)
- retrieving a single cell of data from a MySQL database (PHP)
- Php code confusion. Not sure how to describe (PHP)
- Update entire Mysql DataBase with PhP (PHP)
- mySQL database searching for registration (MySQL)
Other Threads in the PHP Forum
- Previous Thread: About querystring
- Next Thread: user management software
| Thread Tools | Search this Thread |
.htaccess alexa apache api array beginner beneath binary broadband broken cakephp checkbox class cms code convert cron curl database date display dynamic echo email emptydisplayvalue encode error fcc file files folder form forms function functions google howtowriteathesis href htaccess html image images include insert ip javascript joomla key keywords limit link login mail mail() memberships menu mlm multiple multipletables mysql mysql_real_escape_string network oop open passwords paypal pdf php provider query radio random redirect remote rss script search securephp server sessions smtp source space sql strip_tags syntax system table template tutorial update upload url user validator variable video voteup web youtube





