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
Share on other sites

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)

 

 

Link to comment
Share on other sites

You can store the image in the DB as a blob format. However I'd recommend against it, and just store it as a normal image

 

If you do decide to store it in the database, you need to have a separate file that has a header with the correct file type.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.