【问题标题】:php error when trying to echo an image from an external URL尝试从外部 URL 回显图像时出现 php 错误
【发布时间】:2017-07-17 17:26:48
【问题描述】:

在这张图片中它解释得更好一些,但基本上在 html 列中,我需要将来自外部 URL 的玩家头像放置在用户名旁边。但我似乎无法让它工作我想我会在这里问,因为这个社区很有帮助这是代码:

<?php
$connection = mysql_connect('REMOVED', 'REMOVED', 'REMOVED'); 
mysql_select_db('stats');

$query = "SELECT * FROM stats"; 
$result = mysql_query($query);

while($row = mysql_fetch_array($result)){   
echo "<tr><td><img src="'. "https://crafatar.com/avatars/" . $row['uuid'] .
 '">" . $row['name'] . "</td>";  
 echo "<td>" . $row['PLAYER_KILLS'] . "</td>";  
 echo "<td>" . $row['MOB_KILLS'] . "</td>";  
 echo "<td>" . $row['DEATHS'] . "</td>";  
 echo "<td>" . $row['DAMAGE_DEALT'] . "</td>";  
 echo "<td>" . $row['DAMAGE_TAKEN'] . "</td>";  
}



mysql_close(); 
?>

【问题讨论】:

  • 如果某个答案解决了您的问题,请考虑接受该答案。以下是meta.stackexchange.com/questions/5234/… 然后返回此处并对勾号/复选标记执行相同操作直到它变为绿色的方法。这通知社区,找到了解决方案。否则,其他人可能会认为问题仍然悬而未决,可能想要发布(更多)答案。您将获得积分,并鼓励其他人帮助您。 欢迎来到 Stack!

标签: php html url


【解决方案1】:

PHP 会在括号中插入变量,因此您可以转义 src 标记中的引号,然后将变量括在 {} 中:

echo "<tr><td><img src=\"https://crafatar.com/avatars/{$row['uuid']}\">{$row['name']}</td>";

不需要串联,代码更简洁。这是EXAMPLE

结果是:

<tr><td><img src="https://crafatar.com/avatars/42">foo</td>

警告

您应该 stop using mysql_* functions These extensions 已在 PHP 7 中删除。了解PDOMySQLiprepared 语句并考虑使用PDO,it's really pretty easy.

【讨论】:

  • 谢谢!我一定会附上它们
  • 抱歉,我是新来的,我不确定这里的一切如何,再次感谢! PS。改变了我的答案
【解决方案2】:

这应该可行:

echo "<tr><td><img src=\"https://crafatar.com/avatars/\"" . $row['uuid'] . ">" . $row['name'] . "</td>"; 

使用\" 转义"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多