如何将PHP变量作为src属性的值回显< img src [英] How to echo <img src with PHP variables as values of the src attribute

查看:52
本文介绍了如何将PHP变量作为src属性的值回显< img src的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在PHP中回显图像时出现错误.这是我的代码:

I am getting error when I try to echo image in PHP. Here is my code:

$sql = "SELECT id, title, filename FROM images";
$result = $conn->query($sql);


if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
    $pathx = "directory/images/";

    echo '
    <img src = "'$pathx'.'$row["filename"]'">
    ';
    //filenames in database end with appropriate image extensions like .png

}

我收到此错误:解析错误:语法错误,意外的"$ pathx"(T_VARIABLE),预期为,"或者 ';'在第29行的C:\ xampp \ htdocs \ display.php中

我从此处检查了Stackoverflow上类似问题的解决方案,并实现了一些公认的答案,但我的问题仍未解决.

I have checked solutions from similar questions from here on Stackoverflow and implemented some of the accepted answers still my problem is not solved.

同时,下面的代码也可以正常工作.那为什么上面的那个不起作用?

meanwhile something like the code below works well. So why is the one above not working?

echo'<img src = "directory/images/girl-g2109_5_720.jpg">';

推荐答案

这样做

$pathx = "directory/images/";
$file = $row["filename"];

echo '<img src="'.$pathx.$file.'">';


输出

<img src="directory/images/girl-g2109_5_720.jpg">

这篇关于如何将PHP变量作为src属性的值回显&lt; img src的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆