dtg78700 2014-04-29 07:55
浏览 21
已采纳

PHP代码没有做我想要的

I currently have this code. its supposed to get a name from my MySQL database in the row "name" then put "name" into the line of code here:

    echo "<img src="http://cravatar.eu/avatar/"name"/50.png">"

but its not working can someone point out whats going wrong it is just keeping $row ['name'] where I want the name to be. I want it so it gets the name from the database and puts the name in the avatar/name/50.png.

Also, I have multiple names in my database; how do I add a picture for all of them? I want it to be a 2-row, 30-column table using the 60 most recent entries to the recentplayers table in my database.

Here is my name.php if that helps:

    <?php
    $con = mysql_connect("host","user","password");
    if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("quollcr1_hub", $con);

    $result = mysql_query("SELECT * FROM recentplayers");
    echo "<img src="http://cravatar.eu/avatar/$row ['name']/50.png">"

    while($row = mysql_fetch_array($result))
    {
      echo "<tr>";
      echo "<td>" . $row['name'] . "</td>";
      echo "</tr>";
    }
    echo "</table>";

    mysql_close($con);
    ?>
  • 写回答

3条回答 默认 最新

  • donglu9978 2014-04-29 08:02
    关注

    You need to fetch the $row before using it, try this:

    $con = mysql_connect("host", "user", "password");
    if (!$con) {
        die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("quollcr1_hub", $con);
    
    $result = mysql_query("SELECT * FROM recentplayers");
    $row = mysql_fetch_array($result);
    if ($row) {
        echo '<img src="http://cravatar.eu/avatar/' . $row ['name'] . '/50.png">';
        echo "<table>";
        echo "<tr>";
        echo "<td>" . $row['name'] . "</td>";
        echo "</tr>";
    
        while ($row = mysql_fetch_array($result)) {
            echo "<tr>";
            echo "<td>" . $row['name'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    }
    
    mysql_close($con);
    

    *Update

    To show image for every row you can do something like this:

    <?php
    $con = mysql_connect("host", "user", "password");
    if (!$con) {
        die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("quollcr1_hub", $con);
    
    $result = mysql_query("SELECT * FROM recentplayers");
    echo "<table>";
    while ($row = mysql_fetch_array($result)) {
        echo "<tr>";
        echo "<td>" . '<img src="http://cravatar.eu/avatar/' . $row ['name'] . '/50.png">' . "</td>";
        echo "<td>" . $row['name'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
    mysql_close($con);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题