0

I have this short script that instead of displaying the images, displays the raw biary data stored in the blob. Where am I going wrong please?

<?php
@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());
@mysql_select_db($database) or die("Can not select the database: ".mysql_error());
$result = mysql_query("SELECT * FROM image  ORDER BY image_id"); 
while($row = mysql_fetch_array($result)){ 
header("Content-Type: {$row['mime_type']}");
echo $row["pix"]; 
}
?>

Thanks in advance

Stephen

3
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by stephen_UK
0

Are you trying to view the images via an HTML page? If yes, do you know what is the SYNTAX for an HTML Image?

0

The full page is here

<html>
<body>
<?php
$host="xxxxx";  // Database host
$database="xxxxx";		// Database name
$username="xxxxx";     	// Database username
$password="xxxxx";    	// Database user password
$table="image";  // Table Name 
@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());
@mysql_select_db($database) or die("Can not select the database: ".mysql_error());
$result = mysql_query("SELECT * FROM image  ORDER BY image_id"); 
while($row = mysql_fetch_array($result)){ 
header("Content-Type: {$row['mime_type']}");
echo $row["pix"]; 
}
?> 
</body>
</html>

Stephen

0

OK, so if you know HTML, then you should know that the syntax for an image is:

<img src="http://yoursite.com/" alt="some image description here"/>

What you need to do is make sure that the [B]SRC[/B] attribute points to some php page (let's call it imageRetriever.php) and that each url contains a unique ID to the image you want - ex:

<!-- You need to update the code you posted above so that it generates the following markup for EACH of the images. Given my example below, the assumption is that there would be only three images in your query result, but in your case it may be more or less. The 3, 12, and 47 I chose at random, but in your case these would be the unique ids for each of your images. -->
<img src="http://yoursite.com/imageRetriever.php?id=3" alt=""/>
<img src="http://yoursite.com/imageRetriever.php?id=12" alt=""/>
<img src="http://yoursite.com/imageRetriever.php?id=47" alt=""/>

Then in imageRetriever.php you need to use $_GET['id'] and use that value to query the db and send the binary data with the appropriate content-type (basically what you have above, but you need to do it for the reqquested image)

0

Storing images in a blob field is ok, but could you not keep the images in a folder with custom names and pointers to said files in a DB? perhaps it would be less stressful for the poor DB.

0

Thank you for that. It all makes sense now. Yes I was considering your latter suggestion. Many thanks for your information.
Stephen

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.