为什么图像不显示只显示字符

时间:2014-09-30 11:17:17

标签: php mysql

我可以记录和显示数据库中的图像。但是我试图通过下拉列表进行搜索,然后显示图像,但只显示字符。

你能不能帮我说一下我失败了?

对于给您带来的不便,我深表歉意。

非常感谢你。

test.php的

<?php
mysql_connect("localhost","root","****");
mysql_select_db("Database");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
function showdetails(str)
{
var xmlhttp;
if (str==0)
  { 
 alert("Please select an Id");
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("showresult").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","test2.php?id="+str,true);
xmlhttp.send();
}
</script>
</head>

<body>
Image id:<select name="id" id="id" onchange="showdetails(this.value)">
                <option value="0">Select Image id</option>
                <?php

                $sql="select idImage from images";
                $qry=mysql_query($sql);
                $num=mysql_num_rows($qry);
                if($num>0)
                {
                    while($res=mysql_fetch_array($qry))
                {
                ?>
                <option value="<?php echo $res['idImage'];?>"><?php echo $res["idImage"];?></option>
                 <?php
                }
                }
                 ?>
             </select>
             <div id="showresult" align="justify"></div>
</body>
</html>

TEST2.PHP

<?php
mysql_connect("localhost","root","***");
mysql_select_db("Database");

$id = $_REQUEST["id"];
$sql="select * from images where idImage='$id'";
$qry=mysql_query($sql);
$num=mysql_num_rows($qry);
if($num>0)
{
    while($res=mysql_fetch_array($qry))
    {
        $image = $res['image'];
        header("Content-type:image/jpeg");
        echo $image;

    }

}
?>

enter image description here

但如果我在具有特定ID的数据库中进行搜索,则图像会显示在屏幕上。

我无法理解我在哪里。

我尝试编码和解码64 bur,结果是一样的。

mysql_connect("localhost","root","***");
mysql_select_db("database");


$sql="select * from images where idImage='201400040'";
$qry=mysql_query($sql);
$num=mysql_num_rows($qry);
if($num>0)
{
    while($res=mysql_fetch_array($qry))
    {
        $image = $res['image'];
        header("Content-type:image/jpeg");
        echo $image;

    }

}
?>

enter image description here

3 个答案:

答案 0 :(得分:1)

您需要告诉浏览器它是一张图片。我们使用<img>标记。

图片代码也需要来源。在你的情况下。这是数据:

<img src="data:$ActualData" />

$ actualData应该替换为该字符串。在您的代码中:

<div id="showresult" align="justify">
    <img id="my-image" src="" />
</div>

然后你的javascript会改变src:

document.getElementById("my-image").src = 'data:'+xmlhttp.responseText;

错误解决方案

我认为这是错误的解决方案。此时您正在处理图像的所有提取。浏览器可以为您做的事情......所以让它处理所有这些。

只需更改图像的src即可。浏览器将完成剩下的工作。

function showdetails(str) {
    var xmlhttp;
    if (str==0) { 
        alert("Please select an Id");
    }

    document.getElementById("my-image").src = 'test2.php?id='+str;
}

答案 1 :(得分:1)

您需要替换此

<div id="showresult" align="justify"></div>

用这个

<img id="showresult" align="justify" SRC='emptyimage.jpg'/>

并将showdetails函数更改为

function showdetails(str){
  document.getElementById('showresult').src = "test2.php?id="+str;
}

我没有测试此代码但应该可以使用。

答案 2 :(得分:-1)

尝试这只是在你的idImage之前添加一列(别忘了) 只需复制并贴上这个......

test.php的

<?php
mysql_connect("localhost","root","");
mysql_select_db("Database");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
function showdetails(str)
{
var xmlhttp;
if (str==0)
  { 
 alert("please select an Id");
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("showresult").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","details.php?id="+str,true);
xmlhttp.send();
}
</script>
</head>

<body>
Hero Name:<select name="id" id="id" onchange="showdetails(this.value)">
                <option value="0">Select Hero Name</option>
                <?php

                $sql="select id,name from images";
                $qry=mysql_query($sql);
                $num=mysql_num_rows($qry);
                if($num>0)
                {
                    while($res=mysql_fetch_array($qry))
                {
                ?>
                <option value="<?php echo $res['id'];?>"><?php echo $res["name"];?></option>
                 <?php
                }
                }
                 ?>
             </select>
             <div id="showresult" align="justify"></div>
</body>
</html>

和TEST2.php这样的

<?php
mysql_connect("localhost","root","");
mysql_select_db("Database");
$id = $_REQUEST["id"];
$sql="select * from images where id='$id'";
$qry=mysql_query($sql);
$num=mysql_num_rows($qry);
if($num>0)
{
    while($res=mysql_fetch_array($qry))
    {
?>
<img src="foldername/<?php echo $res['photo'];?>" height="300" width="300"/>
<?php
    }
}
?>
相关问题