Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
687 views
in Technique[技术] by (71.8m points)

how to display images from database in php?

i write bellow code to display images from database

<?php
mysql_connect("localhost","root","");
mysql_select_db("test");
$qry = "select id from image";
$res = mysql_query($qry) or die(mysql_error());
while($row = mysql_fetch_array($res))
{
  echo "<img src=image.php?id='$row[0]'>";
}
?>

Image.php

<?php
$query = mysql_query("SELECT img FROM images WHERE id = ".$_GET['id']);
$row = mysql_fetch_array($query);
if($type=="pjpeg")
$type="jpeg";
header("Content-type:$type");
echo $row['img'];
?>

but this will not work. it display blank image icon.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You also might use base64 encoding to build in the image. Like

<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAWgBaAAD/4gxYSUNDX1BST0ZJTEUAAQEAAAxITGlubwIQAAB..." />

UPDATE, base64 encoding example

You can do that easily:

<?php
$imageId = intval($_GET["id"]);

$query = mysql_query("SELECT img FROM images WHERE id = ". $imageId);
$row = mysql_fetch_array($query);

$mime = null;
// place $type init. here
if ($type=="pjpeg") // <<< where do you get $type btw?
    $mime = "image/jpeg";

$b64Src = "data:".$mime.";base64," . base64_encode($row["img"]);
echo '<img src="'.$b64Src.'" alt="" />';
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...