我正在尝试打印其位置保存在我的数据库中的图像,我在数据库中存储了绝对位置而不是相对位置,我浏览了很多问题包括这个 include a PHP result in img src tag 我尝试了提供给问题提问者的所有选项,但我没有得到我的输出,除了图像之外,其余所有内容都显示出来,它显示没有找到文件
这是我的代码,任何帮助将不胜感激
while($result=@mysql_fetch_array($resul,MYSQL_ASSOC)){
$image = $result['image'];
echo $result['company'] . " " . $result['model'] . "<br>" ;
echo '<img src="$image" height="50" width="50" />';
}
我知道我使用的是 mysql 函数而不是 mysqli,但是这段代码永远不会生效。
最佳答案
正如 watcher 所说,PHP 不会在单引号字符串中进行变量插值。
The most important feature of double-quoted strings is the fact that variable names will be expanded.
Read more about strings from the PHP manual.
因此,当您查看 HTML 时,您将从字面上看到:
<img src="$image" height="50" width="50" />
您的代码应该是:
while($result = mysql_fetch_array($resul,MYSQL_ASSOC)) {
$image = $result['image'];
echo $result['company'] . " " . $result['model'] . "<br>";
echo "<img src='$image' height='50' width='50'>";
}
或者,插入数组值:
while($result = mysql_fetch_array($resul,MYSQL_ASSOC)) {
echo $result['company'] . " " . $result['model'] . "<br>";
echo "<img src='{$result['image']}' height='50' width='50'>";
}
如果文件名包含空格或其他特殊字符,您可能需要使用 rawurlencode()
.在这种情况下,您必须连接字符串,因为您正在调用一个返回string
值的函数:
echo "<img src='" . rawurlencode($result['image']) . "' height='50' width='50'>";
关于php - 从 php 模块中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18133304/