【问题标题】:PHP showing image is not workingPHP显示图像不起作用
【发布时间】:2012-10-04 08:21:57
【问题描述】:

您好,我有一个网站,可以向用户显示他们自己的个人资料图片。但我无法这样做,我已经构建了一个代码,但它不起作用并给出错误Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/u522159750/public_html/users/myaccount.php on line 156$sql = mysqli_query($link, "SELECT id, name, avatar FROM users WHERE id="'.$id.'" ") ;这一行任何人都可以建议我可能是什么原因。显示图像的代码如下---

    <p>
 <?php
 //We check if the users ID is defined

$db_host = "xxxxxxxxxxxx"; 
$db_username = "xxxxxxxxxxxxxx"; 
$db_password = "xxxxxxxxx"; 
$db_database = "xxxxxxxxxxxxxx"; 

$link = mysqli_connect($db_host,$db_username,$db_password) or die("Cannot connect"); 
mysqli_select_db($link, $db_database) or die("Cannot select the database");
$sql = mysqli_query($link, "SELECT id, name, avatar FROM users WHERE id="'.$id.'" ") ;
while($result = mysqli_fetch_object($sql)):
<a href="http://www.mysite.tk/users/myaccount.php"> <img src="<?php echo $result->avatar; ?>" alt="<?php echo $result->name; ?>" width="200" height="300" /></a>
?> </p>
<?php   endwhile; ?>

【问题讨论】:

    标签: php html image user-profile


    【解决方案1】:

    您打算输出图像链接的部分是html,应该在php部分之外,或者打印出来,如下所示。

     <?php
     //We check if the users ID is defined
    
    $db_host = "xxxxxxxxxxxx"; 
    $db_username = "xxxxxxxxxxxxxx"; 
    $db_password = "xxxxxxxxx"; 
    $db_database = "xxxxxxxxxxxxxx"; 
    
    $link = mysqli_connect($db_host,$db_username,$db_password) or die("Cannot connect"); 
    mysqli_select_db($link, $db_database) or die("Cannot select the database");
    $sql = mysqli_query($link, "SELECT id, name, avatar FROM users WHERE id="'.$id.'" ") ;
    while($result = mysqli_fetch_object($sql)) {
        print '<a href="http://www.mysite.tk/users/myaccount.php"> <img src="'.$result->avatar.'" alt="'.$result->name.'" width="200" height="300" /></a>';
    }
    ?>
    </p>
    

    【讨论】:

    • 哦,你不需要打印那么多html,他的做法是正确的,只是他失踪的路径
    • 你确定吗?对我来说,我看起来一点也不合适。
    • 哦,是的,对不起,他需要echo out...对..但不是用php打印出html,我想告诉你
    【解决方案2】:

    在浏览器上查看后,检查每张图片的url src,是不是在正确的路径?并确保图像存在于 src 路径中。

    【讨论】:

    • 在查看页面源代码时看起来像这样
    【解决方案3】:

    我不会向您展示代码或示例,但这些建议可能有用:

    db 中的头像列有什么用?它是否采用图像名称或名称。扩展名 或位置/名称.扩展名

    如果它只需要名称:那么你必须在 php 示例中定义位置

    echo '<a href="#" > <IMG src="location/'.$avatar.'.jpg" alt="..." 
    hight="200"width="200"/></a>';
    

    但请注意:在这种情况下,所有头像必须是相同的扩展名(*.jpg)

    如果它需要 name.extension 那么你只需要定义位置

    如果它需要 location/name.extension 那么你的问题不是 $avatar

    但仍然可以是头像的名字本身一定不要留下头像的名字包含 空格或非英文字母字符。你必须重命名它们。

    【讨论】:

      猜你喜欢
      • 2010-10-08
      • 1970-01-01
      • 2020-09-19
      • 2016-04-14
      • 2013-08-09
      • 1970-01-01
      • 1970-01-01
      • 2022-10-12
      • 1970-01-01
      相关资源
      最近更新 更多