0

Can someone help me with this code? The problem is it doesn't show the image but this weird stuff: OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOÿÀx !ÿÄ¢ }!1AQa"q2‘¡#B±ÁRÑð$3br‚%&'()*456789:CDEFGHIJSTUVW

Here is my code:

 <?php
//mysql connect database
mysql_connect('localhost','root','password') or die(mysql_error());
mysql_select_db('database') or die(mysql_error());
$res=mysql_query('select * from img');
while($row=mysql_fetch_array($res))
{
echo "<div id='image'>";
echo "<p/>";?> <img src="<?php echo $row['image'];?>" height='300px' width='468px'>";<?php
echo "<p/>"; echo $row['name'];
echo "</div>"; 
}
?>
2

2 Answers 2

2

<img> expects a URL pointing at where the image file is. You're trying to dump the raw binary garbage of the image into that image tag. That garbage will naturally contain " and > chars in it, and "close" your img tag, letting the rest of the garbage be treated as plain text.

You either have to serve up your image via separate script, e.g.

html:

<img src="pic.php?id=foo">

php:

header('Content-type: image/jpeg');
echo get_blob_from_database($_GET['id']);

Or embed the image inside the html properly:

<img src="data:image/jpg;base64,<?php echo base64_encode($row['image']); ?>">

And neither of these is particularly a good solution. Storing images directly in the database is almost never a good idea.

Sign up to request clarification or add additional context in comments.

Comments

0

it's better to save your images in folder rather than save in blob format

you can save the file name in database table and save the images in folder

when you call the filename, you can combine it with src from and get the content

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.