Member Avatar for stephen_UK

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

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

Member Avatar for stephen_UK

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

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)

Member Avatar for diafol

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.

Member Avatar for stephen_UK

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

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.