douniangliao4327 2017-12-07 05:16
浏览 93
已采纳

如何用PHP变量作为src属性的值回显<img src [重复]

This question already has an answer here:

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

}

And I am getting this error: Parse error: syntax error, unexpected '$pathx' (T_VARIABLE), expecting ',' or ';' in C:\xampp\htdocs\display.php on line 29

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">';
</div>
  • 写回答

4条回答 默认 最新

  • dongzhan0624 2017-12-07 05:21
    关注

    Do like this

    $pathx = "directory/images/";
    $file = $row["filename"];
    
    echo '<img src="'.$pathx.$file.'">';
    

    Output

    <img src="directory/images/girl-g2109_5_720.jpg">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端