Jump to content

[SOLVED] display images with php mysql


priyank

Recommended Posts

i can see characters instead of image using below code. i used blob data type for storing images in mysql.

 

// Performing SQL query

$query = 'SELECT Image FROM tableImages';

$result = mysql_query($query) or die('Query failed: ' . mysql_error());

 

// Printing results in HTML

 

list($Image) = mysql_fetch_array($result);

 

 

echo $Image; //print or return image

Link to comment
https://forums.phpfreaks.com/topic/153589-solved-display-images-with-php-mysql/
Share on other sites

It seems like you've tried storing the image itself in the database. That or something else has gone wrong that i cant think of.

 

You can't store the image itself in the database, you can store a name value and then refer to it on the servers location like http://www.exa mple.com/images/img.jpg

This is hmtl page it has button and iframe when button is clicked it activate showImages.php which shows images

 

<p><form action="showImages.php" method=GET target="imageFrame" name="show_Image">

 

<input type="submit" name=sammount value="Next" size="1">

 

</form>

<iframe name="imageFrame" align="middle" height="600px" width="500px" border="1">

 

</iframe>

 

 

showImage.php has

 

$query = 'SELECT Image FROM tableImages';

$result = mysql_query($query) or die('Query failed: ' . mysql_error());

 

// Printing results in HTML

 

list($Image) = mysql_fetch_array($result);

 

 

echo $Image; //print or return image

 

mysql

Field  Type  Function  Null  Value

imageID int(100)

Image blob Binary - do not edit (0 B)  (Max: 65,536B)

ImageDescription varchar(200)

 

 

Here is a sample script:

<?php
$id = intval($_GET['id']);
// database connection here...

$result = mysql_query("SELECT * FROM `images` WHERE `id` = ".$id." LIMIT 1", $conn) or die(mysql_error());
if(mysql_num_rows($result)>0) { 
$row = mysql_fetch_assoc($result);
header( "Content-type: ".$row['filetype']."");
echo $row['image'];
}
?>

 

id would be the image id, filetype would contain the Content-type, and image would contain the blob of the image.

 

Then in context:

<img src="showimage.php?id=1" alt="some image">

 

Again, I'd strongly recommend just using normal images, and not storing them in the db since it would most likely cause a lot of strain on a site.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.