这个高分表代码有什么问题?

时间:2017-01-27 20:41:11

标签: javascript php mysql

昨天我写了一些javascript / php来从名为' users'的表中检索信息。在MySQL数据库中。它需要用户名和他们的财富'并按分数排名。但是,下面的代码除了标题之外什么都没有显示。它有什么问题?感谢。

<div id="board">
    <table border="1" cellspacing="0" cellpadding="2" width="620"><tbody>
            <thead>
            <tr>
                <td>Username</td>
                <td>Clicks</td>                  
            </tr>
        </thead>
        <tbody>
            <?php

            $con = mysqli_connect('localhost','xxxx','xxxx','xxxx');

            if (!$con) {
            die('Could not connect: ' . mysqli_error($con));
            } else {

            mysql_select_db("users");
            $results = mysql_query("SELECT username, wealth FROM users ORDER BY wealth DESC LIMIT 10");

            while($row = mysql_fetch_array($results)) {
            $username = $row['username'];
            $wealth = $row['wealth']; } 
            }
            ?>
            <tr>
                <td><?php echo $username;?></td>
                <td><?php echo $wealth;?></td>
            </tr>
        <?php               
            mysqli_close($con);            
            ?>
    </tbody>
</table>

1 个答案:

答案 0 :(得分:-1)

您需要在while循环中回显行。

&#13;
&#13;
<div id="board">
    <table border="1" cellspacing="0" cellpadding="2" width="620"><tbody>
            <thead>
            <tr>
                <td>Username</td>
                <td>Clicks</td>                  
            </tr>
        </thead>
        <tbody>
            <?php

            $con = mysqli_connect('localhost','xxxx','xxxx','xxxx');

            if (!$con) {
            die('Could not connect: ' . mysqli_error($con));
            } else {

            mysql_select_db("users");
            $results = mysql_query("SELECT username, wealth FROM users ORDER BY wealth DESC LIMIT 10");

            while($row = mysql_fetch_array($results)) {
               $username = $row['username'];
               $wealth = $row['wealth']; 

               echo "<tr><td>$username</td><td>$wealth</td></tr>";
            }
          
            mysqli_close($con);  

            echo "<script> var results = $results; console.log(results);</script>";          
            ?>
    </tbody>
</table>
&#13;
&#13;
&#13;