图像无法显示,因为它在PHP中的conain错误

时间:2016-06-25 05:58:06

标签: php mysql

$result = mysql_query("select profilepic from `profile` where id = '$pid' ");

if ($result) {
    if ($row = mysql_fetch_array($result)) {
        $img = $row["profilepic"];
    }
}

header("Content-type: image/jpeg");
echo $img;

1 个答案:

答案 0 :(得分:1)

您需要创建另一个PHP脚本来返回图像数据(getImage.php)。将主文件更改为

<body>
<img src="getImage.php?id=1" width="175" height="200" />
</body>

然后getImage.php是

$result = mysql_query("select profilepic from `profile` where id = '$pid' ");
if ($result) {
$row = mysql_fetch_assoc($result) ;
$img = $row["profilepic"];
header("Content-type: image/jpeg");
echo $row['img'];
}

如果您想通过列名

获取值,则必须使用mysql_fetch_assoc()
mysql_fetch_row

- 该函数将返回一行,其中值将按SQL查询中的定义顺序排列,并且键的范围将比所选列的数量小0到1。

mysql_fetch_assoc

- 此函数将返回一行作为关联数组,其中列名将是存储相应值的键。

mysql_fetch_array

- 该函数实际上将返回一个数组,其中mysql_fetch_rowmysql_fetch_assoc的内容合并为一个。它将包含数字和字符串键,可让您以最简单的方式访问数据。

建议使用_assoc_row