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条)

报告相同问题?

悬赏问题

  • ¥15 深度学习残差模块模型
  • ¥20 两个不同Subnet的点对点连接
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)