使用php,图像不会从数据库检索到html表单

时间:2014-04-07 09:56:33

标签: php html

  

Getimage.php

<?php
$hostname="localhost";
$username="root";
$password="tiger";

/* @var $dbhandle type */
 $dbhandle = \mysqli_connect($hostname, $username, $password) 
 or die("Unable to connect to MySQL");

/* @var $select type */
$select= \mysqli_select_db($dbhandle,"sample")
     or mysqli_error($dbhandle);
 /* @var $itemId type */
$itemId= (\filter_input(\INPUT_GET,'name'));
$sql="select img from starterveg where itemId=$itemId";
$res2=mysqli_query($dbhandle,$sql);
$row= mysqli_fetch_assoc($res2);
mysqli_close($dbhandle);
header("Content-type: image/jpeg");
echo $row['img'];
?>

<body>
<img src="Getimage.php?itemId=oepsv1086" alt="image" id="img1">
</body>

&GT;  我无法将数据库中的图像显示到html中。而且alt信息只出现在html表单中

1 个答案:

答案 0 :(得分:0)

试试你的getimage.php

    header("Content-type:image/jpeg");
    stripslashes ($row['img']);

    echo $row['img']; 

***建议不要在DB中存储图像。*

Reference

编辑2&gt;&gt;

$itemId= (\filter_input(\INPUT_GET,'name')); 

这应该是

$itemId= (\filter_input(\INPUT_GET,'itemId')); #as you are passing itemid in get

编辑3&gt;&gt;

你错过了引用错误的查询的单引号(&#39;)

应该是

$sql="select img from starterveg where itemId='$itemId'";